lua-users home
lua-l archive

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


>>(Question: in "a = a + 1", should the second 'a' be a valid "declaration"
>>for the first one?)

>>-- Roberto

It seems to me that the key is to treat implicit *assignment* differently
from implicit *reference* as the latter is relatively harmless. If change
your suggestion so that the first *assignment* to a global raises an error
unless explicit, but then allow further assignment within that chunk, that
would certainly go a good way.

So (assuming a is not already a local):

--start of chunk
a = a + 1 -- Error: attempt to assign to global 'a' without explicit
reference: use _G.a

--start of chunk
_G.a = a + 1 -- Valid provided global 'a' already exists, else Runtime
Error: attempt to add 1 to nil.
a = a + 2    -- Now valid because _G.a has already been assigned once in
this chunk.

I do not see that it needs a 'global' keyword - if implicit reference is
always allowed, _G will be visible and this syntax makes the programmer
think about what he is actually doing.

However, I still think the suggestion of compiling implicit variables as
upvalues of the enclosing function (or chunk) is worth further study - with
a "C" API override for the incremental compilation crowd.

-- John