Lua local question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

Re: Lua local question

Post by pancakepalace »

I don't think there is, but I don't see why this would be necessary. Just declare the variable at the top of the file.

Code: Select all

local b

function lol()
 b = somethingRather
end
I think this is the clearest way no. Declare variables in the scope that they will operate.
User avatar
Hydra
Prole
Posts: 5
Joined: Wed Aug 17, 2011 1:59 am
Location: Canada
Contact:

Re: Lua local question

Post by Hydra »

pancakepalace wrote:I don't think there is, but I don't see why this would be necessary.
Depends on what you're doing, here's a small example:

Code: Select all

local x = 0

local Add = function(number)
	x = x + number
	return x
end
Is it the best example of a use for this? Not really, but you get the point.
pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

Re: Lua local question

Post by pancakepalace »

I'm not too sure what's the problem with your example.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Lua local question

Post by bartbes »

Yeah, you're asking us how to affect a 'file-local' local and then complaining it does? You lost me, anyway.
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: Lua local question

Post by Rad3k »

To clear up some confusion:
In Lua, accessing a table field always incurs some overhead - Lua has to find the key/value pair you're looking for. The more entries in that table, the larger the overhead (my guess is that it grows logarithmically). Like Lua manual says, global environment is an ordinary table, so each time you invoke a global variable, you're implicitly indexing a table. Now, if you declare some variable local, it's only accessible from its enclosing block, but the access to it is direct - it doesn't involve indexing any table. Also, each time you use 'local' keyword, you create a new local variable, even if there already exists a local variable with the same name. The old one will just become inaccessible.

As for function arguments - inside a function body, all the arguments are local variables. So, it's not only redundant to write:

Code: Select all

function fun (arg)
  local arg = arg or default_value
  do_stuff
end
but it's also undesirable - it needlessly creates another local variable. It's roughly equivalent to:

Code: Select all

function fun (arg1)
  local arg
  if arg1 then
    arg = arg1
  else
    arg = default_value
  end
  arg1 = nil
  do_stuff
end
Now, when you write:

Code: Select all

function fun (arg)
  arg = arg or default_value
  do_stuff
end
it's equivalent to

Code: Select all

function fun (arg)
  if arg then
    arg = arg
  else
    arg = default_value
  end
  do_stuff
end
Which, I believe, can be optimized by Lua compiler to just:

Code: Select all

function fun (arg)
  if not arg then
    arg = default_value
  end
  do_stuff
end
I hope it helps :)
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Lua local question

Post by T-Bone »

pancakepalace wrote:I'm not too sure what's the problem with your example.
bartbes wrote:Yeah, you're asking us how to affect a 'file-local' local and then complaining it does? You lost me, anyway.
If you're talking to me, then first of all I'm not complaining. i was just wondering if there was syntax for declaring something "file-local". Turns out there isn't, which isn't a problem :neko:
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Lua local question

Post by bartbes »

There is no such concept as a file, every file is just another block of code. So in the end what matters is where you define it/first use it.
As a language with lexical scoping it means it basically searches from the inside out, so where you last defined it as local, is what it will use. The solution to your 'problem' is indeed, to define the variable as local at the scope you want it, the file scope, in your case.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Lua local question

Post by T-Bone »

bartbes wrote:There is no such concept as a file, every file is just another block of code. So in the end what matters is where you define it/first use it.
As a language with lexical scoping it means it basically searches from the inside out, so where you last defined it as local, is what it will use. The solution to your 'problem' is indeed, to define the variable as local at the scope you want it, the file scope, in your case.
That post confused me. First you claim there is no such consept as a file, then you refer to "the file scope". If you declare something as local in one .lua file, can you still reach it from another? I'm still getting used to script languages so I get confused about stuff like this :neko:
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Lua local question

Post by bartbes »

Like I said, a file is like any other chunk of code. You can see this with lua's loadfile and loadstring and love's love.filesystem.load, they all load the code into a function, so whatever is local there, in your case the file, is local to that 'function', and as such can't be accessed outside of the file.
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Lua local question

Post by kraftman »

T-Bone wrote:
bartbes wrote:There is no such concept as a file, every file is just another block of code. So in the end what matters is where you define it/first use it.
As a language with lexical scoping it means it basically searches from the inside out, so where you last defined it as local, is what it will use. The solution to your 'problem' is indeed, to define the variable as local at the scope you want it, the file scope, in your case.
That post confused me. First you claim there is no such consept as a file, then you refer to "the file scope". If you declare something as local in one .lua file, can you still reach it from another? I'm still getting used to script languages so I get confused about stuff like this :neko:
No, if it's local to a file, it can't be reached by other files.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest