lua-users home
lua-l archive

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


> "self:super()" would, I assume, return a table with the object's data but with the functions of the superclass.  Could this be accomplished by making a copy of the object table and setting its metatable to the base class table?

I think that making a copy of the super class is very inefficient and
error prone. What I usually do is to define an attribute in the
derived class pointing to the base class:

--~~--

   function createClass(base)
       local newClass = {} -- Contains class methods
       local classMeta = { __index = newClass }

       if base
       then
           newClass.super = base
           setmetatable(newClass, { __index = base })
       end

--~~--

And then, in my code:

--~~--
Parent = class()
Child = class( Parent )

function Child:init( ... )
  Child.super.init( self, ... )
end
--~~--

Note that when you want to access the super class, you have to index
the the current class ("Child.super"), instead of "self.super",
because self's class can be different than the class you declared the
method.

--rb