|
On 2013-08-23 16:45:09 +0000, Philipp Kraus said:
On 2013-08-23 15:47:34 +0000, Coda Highland said:
On Fri, Aug 23, 2013 at 8:46 AM, Coda Highland <chighland@gmail.com> wrote:
On Fri, Aug 23, 2013 at 8:43 AM, Philipp Kraus
<philipp.kraus@flashpixx.de> wrote:
x = myClass( 123 )
x.test() -- shows nil
Because it needs to be x:test().
/s/ Adam
... although that'll output some table address and not the value of
x.id because you're calling print(self) and not print(self.id).
/s/ Adam
I can do x.test() or x:test(), both calls result nil.
The parameter "self" is always nil, so imho I have a wrong definition in my metatable
I have defined my calls with
local m_metafunctions = { __add = true, __call = nil, __concat = true, __div = true, __gc = nil, __le = true, __lt = true, __mod = true, __mul = true, __pow = true, __sub = true, __tostring = true, __unm = true }
function class( pcClassName, ... )
local loClassDef = {
__classname = pcClassName,
__instancedata = {},
__metadata = {
__mode = "k",
__call = function(self, ...) self:constructor( self.__instancedata, ... ) return self end,
__gc = function(self, ...) self:destructor( self.__instancedata ) end,
__newindex = function(self, key, value)
if type(value) == "function" and m_metafunctions[key] then
self.__metadata[key] = value
else
self.__instancedata[key] = value
end
end,
__index = function(self, key, ...)
local lxReturn = self.__instancedata[key]
if type(lxReturn) == "function" then
return lxReturn( self.__instancedata, ... )
end
return lxReturn
end,
},
constructor = function( ... ) end,
destructor = function( ... ) end
}
return setmetatable( loClassDef, loClassDef.__metadata)
end
I run this with
local myClass = class("MyClass")
function myClass:constructor( self, pxID )
self.id = pxID
end
function myclass:test(self, ...)
print(self)
end
local x = myClass( 123 )
x:test()
the print(self) results always nil. the __call and __gc works well also the __newindex, but the __index does not work
Thanks for help
Phil