lua-users home
lua-l archive

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

Mark Meijer [] said:
I certainly don't hope the _G.myglobal syntax is going to be required.
I prefer the explicit forward declaration way. Sure, _G sticks out, and
that's because it's ugly. Forward declarations stick out well enough and are
much nicer and cleaner, imho.

Also, I think what you want is to handle initialization differently.
Not assignment. And reference (in the sense you meant it) is only harmless
if the variable has already been initialized. The forward declaration
approach handles this nicely.

I'm not sure what you mean by "the forward declaration approach": do you
mean 'global x' on analogy with 'local x'? If this applies to reference as
well as assignment does it mean that you have to explicitly declare
everything, even library functions?

When I thought this out originally, I realised that all the compiler can do
once it has exhausted a search for a name in its local tables is to compile
it blindly as a late bound global (table lookup). It cannot know if the
global will exist at runtime and therefore if it will be initializing or
assigning. It can however distinguish whether it is programming to reference
(read) a global or to assign (write if it exists, create and write if it
does not).

In my view, you should not be allowed to assign a value to a global variable
without being explicit about it. I have no problem if the syntax is "global
x = 1" or "getfenv().x = 1" or "_G.x = 1", although the latter two are more
versatile and avoid another reserved word. Also, I see the argument that you
do this once in a chunk and then you can implicitly assign it from there on.

I do not see why you say "_G.x" is ugly - unless it is the leading
underscore which could easily be fixed.

I do not get why Roberto wants to limit the forward declarations to the
chunk level: if you are going to have them, why not let them work the same
as locals, so the declaration has lexical scope? But they have to lock out
assignment (write) only, or it will be as bad as "C" with every chunk
starting with huge imports of header files!

- John