lua-users home
lua-l archive

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

My preferred version of the global handling would be a way to switch on a
requirement that globals be explicitly declared. _G isn't really any more
special than anything else. I would actually like to pass a list of
pre-defined globals to the chunk compiler and then have it complain about
any others since it feels a bit odd to have to start almost every chunk

    global require

(Or in the case of Lightroom "global import".)

The benefit of requiring global declarations is that it makes one sort of
error easy to catch at compile time.

Even better would be a syntax that resulted in one being encouraged to write
the equivalent of:

    local table = table

since this is almost invariably a performance boost.

On the other hand, this all needs to be optional. It would be incredibly
annoying in an interactive environment to have to re-declare globals with
each chunk.

The interface I'm thinking about might be something like the following:

    loadstring( chunk_text, {
        name = chunk_name,
        globals = { "require },
            -- pre-declared globals
        env = < environment for the compiled chunk >,
            -- saves the use of setfenv on the function
        strip_debug = false,
            -- should we strip debug symbols from the result?
        token_filter = ...
            -- the token filter to install while compiling
    } )


P.S. Just to re-iterate for those who want "local" to go away for local
declarations, the problem is that one cannot reliably intuit the scope for
local variables without explicit declarations.