lua-users home
lua-l archive

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

I'm using Lua to replace a scripting language (which I'll call FOO) in an existing application. 
For the most part, translating FOO scripts into Lua is trivial, but there a few cases where FOO's
semantics are different enough to make my life difficult.  The first is that variables need not
exist before they are used in an expression. For instance:

  x = x + 1

In FOO, when x is encountered in the rhs expression and found not to exist, it is created and
initialized to 0.  After this statement, x will have the value 1.

I can emulate this behavior in Lua with:

  metatable (
    globals(), {
    index = function(t,k) 
      return 0 

The second situation is:

  a[0] = 1

Again, in FOO, if 'a' does not exist before this statement, it is created.  I can emulate this

  metatable (
    globals(), {
    index = function(t,k) 
      local nt = {}
      return nt 

My problem is that I can't have both. I've resigned myself to choosing one or the other, and
trying to fix the other case some other way... but I thought I would post here first, in case
someone knows a trick that would let me have my cake and eat it, too. ;)


Do You Yahoo!?
Yahoo! Greetings - Send FREE e-cards for every occasion!