I implemented a small OOP library in Lua, and two things are not quite right yet. I need your advice!
How to call super()?
I need to make a choice. The three arguments I need to resolve a call to super() are:
- The class from where the call is being made (CallerClass)
- The instance to be passed (self)
- The name of the method (method)
I hesitate between these three forms:
--# Current way:
self:super(CallerClass):method()
--# Variant, which I now find cleaner:
CallerClass:super(self):method()
--# Python style, which is nice too:
super(CallerClass, self):method()
Which one looks nicer and or easier to remember to you?
Do I need a Class symbol?
In the current version, the only concept is a table named Object
, which you can subclass. I have another version where I introduced a Class symbol.
Its use is to tell instances from classes. I.e. :
assert(Object:isKindOf(Class))
local object = Object:new()
assert(not object:isKindOf(Class))
I find it very handy to document and enforce that a method must be called from a class, by starting the method with:
assert(self:isKindOf(Class))
Is it usefull in Lua? What do you think? Thanks!