lua-users home
lua-l archive

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


On Apr 26, 2013, at 7:13 AM, Thomas Jericke <tjericke@indel.ch> wrote:

> To me there are still some problems regarding scoping with this, consider:
> 
> if someboolean then
>    global myvar = 0
> end
> 
> print(myvar) -- Is this valid?
> 
> Probably the most consistent way would be, that scoping is used for globals too and you would have to do this:
> 
> global myvar
> 
> if someboolean then
>    myvar = 0
>    global g2
> end
> 
> print(myvar) -- ok
> g2 = 0 -- error

If this is what you want, then the local and global keywords mean the same thing. In expression oriented languages of the ML family, this keyword is called val. It declares a new value in the present scope. So your example becomes:

val myvar

if someboolean then
   myvar = 0
   val g2 = 7
end

I like this approach, but I am not advocating a change to Lua. 

This can also be accomplished with a new operator. In mathematics, := is often used for definitions. 

myvar := nil -- global declaration and initialization

if someboolean then
   myvar = 0 -- assignment
   g2 := 7   -- local declaration and initialization
end

I am not advocating this either, but it may spark some derivative language experiments.

e