I haven't gotten too far, since I've been busy the last few weeks. Everything I have is currently on the google code repo. I have some ideas for implementing OOP, but it is a bit tricky.
what I meant is that __thiscall basically inserts the "this" pointer as
the first argument in each instance method call from the class instance (pseudo code):
is basicallyCode:class SomeClass { void SomeFunction(T1 param1, T2 param2, ...); } SomeClass c = new SomeClass(); c.SomeFunction(param1, param2, ...);
In Lua, OOP has been implemented the same way using tables, just that the "this"Code:SomeFunction(SomeClass& this, T1 param1, T2 param2, ...); SomeClass c = new SomeClass(); SomeFunction(c, param1, param2, ...);
pointer we call "self"
Basically, both comes down to:Code:local SomeClass = {} SomeClass.__index = SomeClass function SomeClass.SomeFunction(self, param1, param2, ...) ... end --[[ The same as: function SomeClass:SomeFunction(param1, param2, ...) ... end ]] local c = setmetatable({}, SomeClass) c:SomeFunction(param1, param2, ...) -- The same as SomeClass.SomeFunction(c, param1, param2, ...)
Code:SomeFunction(SomeClass& this, T1 param1, T2 param2, ...);
But that's cheating! That is the equivalent of calling C an object oriented programming language by passing structure pointers to functions and adding some syntactic sugar. How is inheritance and polymorphism implemented in LUA?
Last edited by jeremic; 04-21-2014 at 08:49 PM.
well, it's tpforums :P
yeah, Lua uses the ':Func' as syntatic sugar for the '.Func(self, ' and speed up the OOP coding.
It's all about controling metamethods of the parent table. Basically, __index is the getter and __newindex is the setter function. There are more metamethods (aka methods to control its attached table) even to operator overloading like __add, __sub, __lt, __lt, __rt, __eq, __neq, ...
I'm a Lua lover, even though I think that implementation is ugly as hell (without proper keywords and so on), but as the Lua author said Lua is meant to be built from the ground up just with the table data structure. Probably that's the reason he stickied this way.