Page 3 of 4

Re: Lua local question

Posted: Fri Aug 19, 2011 9:39 am
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.

Re: Lua local question

Posted: Sun Aug 21, 2011 1:47 am
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.

Re: Lua local question

Posted: Sun Aug 21, 2011 3:44 am
by pancakepalace
I'm not too sure what's the problem with your example.

Re: Lua local question

Posted: Sun Aug 21, 2011 11:26 am
by bartbes
Yeah, you're asking us how to affect a 'file-local' local and then complaining it does? You lost me, anyway.

Re: Lua local question

Posted: Sun Aug 21, 2011 11:31 am
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 :)

Re: Lua local question

Posted: Mon Aug 22, 2011 10:57 am
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:

Re: Lua local question

Posted: Mon Aug 22, 2011 12:19 pm
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.

Re: Lua local question

Posted: Mon Aug 22, 2011 1:06 pm
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:

Re: Lua local question

Posted: Mon Aug 22, 2011 2:04 pm
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.

Re: Lua local question

Posted: Mon Aug 22, 2011 2:59 pm
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.