lua-users home
lua-l archive

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


> On Behalf Of Geoff Leyland
> Sent: maandag 8 oktober 2012 8:39
> 
> On 8/10/2012, at 6:58 PM, Thijs Schreijer <thijs@thijsschreijer.nl>
> wrote:
> 
> > Typex traverses the registry to find the name of the metatable and
> > returns that as a second argument. The results of the registry
> > traversal are then cached to speed-up next searches.
> 
> Nice idea!  Am I right in thinking that it only works for userdata, not
> tables-as-objects?

Yes, only userdata metatables. Metatables on tables do not necessarily have
a name registered in the registry. Though it would be easy to extend it. The
general purpose is only an equality check (or am I overlooking something?)

So to check on equality all that is necessary is doing a tostring() on the
metatable for a unique string. The part not covered would then be the case
where similar 'objects' get a new (but similar) metatable as in;

    function whatever()
      local result
      -- do something
      return setmetatable(result, {value1, value2})
    end

In that case an __type field/function could be used

So adding it up;
 - for userdata and tables, check on __type metatable field
If __type found then
 - if it's a field return its value
 - if it's a function call is with self (the original userdata/table) as
parameter and return that
If __type not found
 - for a userdata look up the metatable name in the registry and return
that.
 - for a table do a raw tostring() (no __tostring metamethod) on the
metatable and return that


Thijs