[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Object references from user data
- From: RLake@...
- Date: Mon, 4 Aug 2003 15:14:49 -0500
> CarMetatable =
> __newindex = function(table, key)
> return rawget(Vehicle, key) or -- "inherit from Vehicle"
> return rawget(Engine, key) or -- "inhereit from Engine"
> return rawget(Wheels, key) end -- "inherent from Wheels"
Yup, it is easy enough. A couple of quibbles:
1) The use of rawget means that you will not be able to chain
inheritance. (i.e. if Vehicle inherits from something, then Car
won't acquire that.) I would just use an index:
Vehicle[key] or Engine[key] or Wheels[key]
2) "return" doesn't actually have a value. I think you probably
didn't mean to type it three times.
3) You have to watch out for the false/nil dichotomy; the code
as presented will treat false as though it were nil, preventing
you from inheriting the value of a boolean member (if the
member's value is false). If that is possible, you need to
write somewhat more elaborate tests, something like this:
local rv = Vehicle[key]
if rv == nil then rv == Engine[key] else return rv end
if rv == nil then rv == Wheels[key] end