[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Inheritance and setmetatable(a,{__index=b})...
- From: Adrian Sietsma <adrian_groups@...>
- Date: Fri, 31 Dec 2004 00:28:03 +1000
duck wrote: (edited - my comments added)
I have code like this:
....
local fruit = {
sort = "mango",
type = "ripe",
taste = "smooth"
}
local ashes = "oz"
class = {
new = new,
repr = repr,
fruit = fruit, -- TABLE ref
ashes = ashes -- STRING ref
}
....
o1.ashes = "england" -- assign string to o1.ashes
o1.fruit.sort = "banana" -- modifies shared (by metatable) "fruit" table
-- lua first looks for a "fruit" element in o1 - doesn't find one.
lua then looks in o1's metatable for "fruit", and finds the local
(shared) fruit table.
it then assigns "banana" to the "sort" member of this table.
if you said
o1.fruit = {}
then o1 would have it's own fruit table, and
o1.fruit.sort = "banana"
would have the expected efect.
the object (via metatable) inherits a REFERENCE to the fruit table, in C
terms.
Adrian