lua-users home
lua-l archive

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

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.