lua-users home
lua-l archive

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


On Thu, 2004-12-30 at 22:15, duck wrote:

> local fruit = {
>         sort = "mango",
>         type = "ripe",
>         taste = "smooth"
> }
> 
> local ashes = "oz"
> 
> class = {
>         new   = new,
>         repr  = repr,
>         fruit = fruit,
>         ashes = ashes
> }
> 
> return class
> ***

> o1.ashes = "england"
> o1.fruit.sort = "banana"

> Does this mean that the object doesn't inherit tables from the parent 
> class, or does it mean that it inherits a "shallow copy" of the table 
> from the parent? Or what? The objects seems to have their own "ashes"
> but not their own "fruit"...


In your new function, you created a new table for each object.
o1.ashes is storing in that via the tricky metamethod.
But o1.fruit.sort is storing in o1.fruit.. which is shared.

There is no copying at all, not even shallow
copy -- the two objects have distinct tables because
YOU created a new table for each one.

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net