lua-users home
lua-l archive

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

Recently I've been toying around with an implementation of overloaded
functions in Lua.  I ran into an issue that must be well-known by anyone
working on Lua extensions.  I found that at the lowest level I need to
provide two implementations for everything: one for the global environment
and another for tables.

Since the table data type is used so effectively to keep Lua simple and
powerful, it made me wonder why use of tables wasn't extended as far as the
global environment.  In other words why does a programmer implementing
extensions have to treat the global environment as a special case?

The solution I'm thinking about is to keep current Lua semantics intact, but
remove all the global operations (listed below) from the language.  Then add
just one function to get a global table value, say globaltable().

    global operation            table "equivalent"
    ----------------            ------------------
    setglobal()                 {table access syntax}
    getglobal()                 {table access syntax}
    rawsetglobal()              rawgettable
    rawgetglobal()              rawsettable
    foreachvar()                foreach
    nextvar()                   next
    setglobal tagmethod         settable tagmethod - roughly
    getglobal tagmethod         gettable tagmethod - roughly

By making this change the Lua language and documentation would be
simplified, and I believe the complexity of implementing extensions would be

It doesn't seem there would be much of a performance hit.  Furthermore for
backward compatibility, all the global operation functions could be
re-implemented using the table equivalents, and the "getglobal" and
"setglobal" tags emulated.  I'm sure that I must be overlooking something...
what is it?

It looks possible to run through an implementation of this within Lua itself
without changing any source code.  Before I run down that road I thought it
would be good to entertain some criticism.

-John Belmonte