I really like how object-oriented programming is described in "Programming in lua" 16.1, 16.2:
http://www.lua.org/pil/16.1.html
http://www.lua.org/pil/16.2.html
and would like to follow this approach. but I would like a little more detail: I would like to have a base "class" called "class", which should be the base of all subclasses, because I want to implement some helper methods there (for example, "instanceof", etc ..) , but essentially it should be as described in the book:
function class:new(o) o = o or {} setmetatable(o, self) self.__index = self return o end
now to my problem:
I would like to have a class "number" that inherits from a "class":
number = class:new()
I would like to define metamethods for operator overloading (__add, __sub, etc.) in this class, so something like:
n1 = number:new() n2 = number:new() print(n1 + n2)
working. It's not a problem. but now I would like to have a third class “money” that inherits from the “number”:
money = number:new{value=10,currency='EUR'}
i introduce some new properties here.
Now my problem is that I can’t get it to work, that “money” inherits all the methods from the “class” and “number”, including all the metamethods defined in the “number”.
I tried several things, such as rewriting "new" or changing metathetes, but I could not get it working without losing the "class" methods in the "money" or losing the "number" metamethods in the "money"
I know that there are many class implementations, but I really would like to stick with a minimal approach to lua itself.
Any help would be greatly appreciated!
Many thanks!