[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Metatables
- From: Eric Tetz <erictetz@...>
- Date: Wed, 27 Feb 2002 15:06:57 -0800 (PST)
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)
rawset(t,k,0)
return 0
end,
})
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
with:
metatable (
globals(), {
index = function(t,k)
local nt = {}
rawset(t,k,nt)
return nt
end,
})
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. ;)
Cheers,
Eric
__________________________________________________
Do You Yahoo!?
Yahoo! Greetings - Send FREE e-cards for every occasion!
http://greetings.yahoo.com