lua-users home
lua-l archive

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


Mark Hamburg wrote:
>
> If you can live with single-inheritance, my scheme using indices in the
> metatable will work with the type-testing technique used in Oberon. Single
> inheritance allows us to know that a class is at a particular depth in the
> inheritance tree. The metatable for a class can then include the appropriate
> light userdata for each class in its inheritance hierarchy. Testing for
> class membership then becomes something like:
> 
>     luaObj.metatable[ classInfo->depth ] == classInfo

What I'm using (in Sol): every class table has an is_<ClassName> field
which is set to true.  Because of the inheritance, these fields are
inherited too.  Checks are simply:

   assert(obj:is_Foo)  -- implies obj:is_Table if Foo is derived from Table
or
   if obj:is_Table then ... end

It's a bit ugly because of the classname namespace and it may be slow
with deep hierarchies (unless the inheritance-resolver dynamically copies
fields to derived classes) but it was so simple to implement and easy to
use... :-)

Ciao, ET.