lua-users home
lua-l archive

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


On 31/05/2012, at 4:52 PM, Coda Highland wrote:

> I'd like it better if it were t.is(string), because then you're not
> using strings for type matching but instead confirming the actual type
> object.

What's your objection to strings?  I've tried variations on the type.isa(object) and isa(type, object) and ended up with what I suggested earlier.

That said, there's no reason not to store metatables in the typeinfo table:

myclass = { __typeinfo = { myclass = true} }
myclass.__typeinfo[myclass] = true
myobject = setmetatable({}, myclass)

function typeinfo(obj)
  return getmetatable(obj).__typeinfo
end

if typeinfo(myobject)[myclass] then
  ...
end

The implementation at [1] stores both metatables and strings, but I think I've only ever used the strings.


[1] https://github.com/geoffleyland/rima/blob/master/lua/rima/lib/object.lua