lua-users home
lua-l archive

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


On 04/25/2013 10:54 PM, Steve Litt wrote:
I would too. If I really want something to be global, I'll

global myvar = 0

It's not like I'm going to have 40 globals anyway, so this won't be
much writing.

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

My favorite approach would be different. All accesses to global would go through the according variables: _G or _ENV respectively. "MyGlobal" is just syntactic sugar for "_ENV.MyGlobal." Now we could have some syntactic sugar where you can leave away the _ENV but not the '.'
.MyGobal = 0

That would still be almost as short as the current syntax. I just don't know if there are some cases where the lexer could get into trouble. But the lexing rule would be '.' followed by letter is a global. If '.' doesn't work, another symbol could be used. The string appending may look a bit bad:

print (.myglobal1...myglobal2) -- Not so nice
print (.myglobal1 .. .myglobal2) -- A little better

It would also help editors a lot if they don't need to do reference resolving to highlight globals.

--
Thomas