lua-users home
lua-l archive

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


mnicolet escribió:

> I still argue that ( from lvm ) if two ´userdata´
> objects have the same metatable ( i,e, they are ´the same type´ ), then
any
> of the methamethods ´pairs´ will be the same, and will be compared as
equal.

Yes, this is true. However, it is too restrictive.

Userdatas have no easy way of keeping non-trivial Lua objects. (Even
strings can be a problem because they have to be protected from garbage
collection.)

It is possible to use the registry, perhaps by using the object itself as a
key, but that would prevent the object from being garbage collected. There
is a workaround for this, but it is even more complicated.

So the simple solution is for each userdata to have its own metatable,
which it can use to keep state information which needs to be shared with
Lua. Unfortunately, that defeats a simple object-equality test on the
metatable, even though the metamethods themselves would still be the same.

In order to support this idiom, it is necessary that the metamethods, and
not the metatable, be compared for equality.

The examples I provided last time were in the way of examining whether even
this test is too restrictive or not.

R.