lua-users home
lua-l archive

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


Gang,

I have a table that represents a rectangle. Normally it'd be set up like:

r = {
   top = 0
   left = 0
   right = 100
   bottom = 100
}

so that if I then:

r.top = r.top + 10
print(r.top)

I get "10"

What I'd like to do however is have the four fields, if nil when referenced, auto default to zero. I've been trying to get this with __index and __newindex in a metatable, but I can't seem to find a way.

In other words, say I do this:

r = {}
r.top = r.top + 10
print(r.top)

I want want lua to successfully realize that r.top is nil and instead of throwing an error, auto init r.top to zero, add 10 to it, and then print the result

Is this possible? I'm staring to confuse myself now more than gain any clarity....

Thx mucho,
Ando

-----------------------
Ando Sonenblick
SpriTec Software
www.spritec.com