lua-users home
lua-l archive

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


On 2013-08-23 10:48:31 +0000, Luiz Henrique de Figueiredo said:



I can call the functions with mytab.func1(), but I would like to use a reference to the table itself (like the self parameter on Python classes):

mytab = {

func1 = function(self, ?) do something end

func2 = function(self, ?) do something other

}


and self should be point to mytab. How can I do this?


Your solution using self works fine. Just call the function with mytab:func1().



The short example works, but on my full example it does not. I have defined:


local myClassDef = {


     __instancedata = {},

     __metadata = {},


     constructor = function(…) end

}


myClass.__metadata = {


      __call = function(self, …) self:constructor(self.__instancedata, …) return self end, 

      __newindex = function(self, key, value) self.__instancedata[key] = value,

      __index = loClassDef.__instancedata  


}


local myClass = setmetatable( myClassDef, myClassDef,__metadata


function myClass:constructor( self, id )

     self.id = id

end


function myClass:test( self )

    print(self)

end



x = myClass( 123 )

x.test() -- shows nil


I try to define my own OOP structure, I have take a look to loop, etc. I would like to push the table itself to each defined method


Tanks a lot for help


Phil