lua-users home
lua-l archive

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


On Mon, July 12, 2010 2:56 pm, Roberto Ierusalimschy wrote:
> The default is no declaration. Only coders concerned about their use
> of globals (e.g., people writing modules or large programs, or people
> complaining about global-by-default ;) would add such thing.
>
>> Perhaps it should be an argument to the load*() function?
>
> Global declaration would be a purely syntactical thing. It does not
> improve "security" in any way; one can always write "_ENV.x" to
> access any global. It would only control the availability of a syntax
> sugar. So, I think it is the one writing the chunk that has a say about
> whether she wants or not the sugar, not the one using the chunk.

I think I like what Roberto Ierusalimschy is suggesting, assuming I'm
understanding it correctly. Here's how I see a "global" declaration
keyword working:

a = 10;         --  a is a global (global assignment checking isn't
enabled yet)
b = 20;         --  b is a global too

global checks;  --  using the global keyword turn global assignment
checking on.
                --  "checks" is a global, but it's just a dummy in this
case and not used.

function hello()
    global  a, b;   --  a and b are globals (same ones as above)
    local   c;      --  nothing new here

    a = b + 1;      --  Without the declaration above, the assignment
would result in a
                    --  compile time error here.
    c = a + b;      --  This line would be OK even without the global
declaration.

    return c;
end

When you "require" a library, the loaded code chunk should revert to the
default (non-checked behavior) for backward compatibility.

Should function declarations (as above) be exempt or should I have written
global function hello()? I kind of like the idea of checking function
declarations as well.

-- 
Juri Munkki