lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>From: Luc Van Den Borre <lucv@criterion.canon.co.uk>
>
>I suppose there's no way of having some static variables local to one file ?

Not exactly.

>I.e. I would have two files that both have a variable called 'name' at the
>top, which can be used by all the functions in that file,
>but the variable has different values in different files...

You can declare local variables in to "top level":

	local a=102.3
	function f() ... end

but you cannot use 'a' inside function bodies.
What you can do is to to use the *value* that 'a' has when 'f' is defined:

	local a=102.3
	function f(x) return %a+x end

'%a' is called an upvalue.

So the value of 'a' is frozen to 102.3.
It's equivalent to having
	function f(x) return 102.3+x end
except that it might be more readble.

Changes to 'a' after 'f' is defined do not affect 'f'.

>Just like in C...

Lua is not C :-)
--lhf