That is, I have to know whether or not capVelocity is in my superclass or in my class and the syntax is different for the two cases (in the use of self vs Thing). It seems to me that this makes subclassing rather problematical and less useful than in other languages one might know.
Apparently the table for Ship does not point to and search the function table for Thing, so that superclass functions are not automatically found??
Am I missing something, or is the above the way of things? Relatedly, why does Thing.init(self) work and Thing:init() not work? I thought they were equivalent?
Ah … issue is the same but somewhat obfuscated in the new Spacewar. I think.
Messages make it seem it can’t be said but load order seems to fix it. Thanks. And thanks to jmv38 for forcing me to build an example that was dumb enough to let you remind me about load order
I have been in the software industry for over a half century and have programmed at least moderately well in APL, Basic, C, C++, C#, FORTRAN, IPL-V, Java, LISP, LSL, Pascal, Ruby, Smalltalk, and surely some I have forgotten.
Nonetheless, I’m not yet grokking everything about Codea/Lua …
If you want to understand the good way to use inheritance, study cargo bot example. Everything works as expected with inheritance, except the init() function of the parent class that must be called as you have done it
parent.init(self)
i’ve tried plenty of tricks to do it a more ‘natural way’, but none works. (some seem to work, but when you have multiple inheritance levels, they dont). That’s just the way to do it.
I have also noticed it is important to do things with 2 other concepts also:
components: add functionnalities with self.something = Something(). It is often more easy to manage compared to force some inheritance process.
mixin: if you want to add a component for its functions, but the call self.something.action() is really heavy and you prefer self.action(), then just copy all the function of Something() into self (except init), that works great. You can even define a class just for being mixed in, without init() function defined.
These 2 additionnal ways are really better than inheritance in 50% of cases (better decoupling), and you must decide yourself on a case by case basis which technique is the best,
@RonJeffries this is just my personnal experienc with codea that i am sharing, learned hands on and the hard way, i am not a programmer in real life. I’ve learned OOP on this forum mainly. But i think this may help you, because that’s from the noob’s pov.
self.vel = self:capVelocity(self.vel + accel + gravity)
is the same as
self.vel = self.capVelocity( self, self.vel + accel + gravity)
capVelocity, if not defined in Ship, will be looked for into Things, so that is correct
However for me the problem is that this works:
self.vel = Thing:capVelocity(self.vel + accel + gravity)
it is equivalent to
self.vel = self.capVelocity( Thing, self.vel + accel + gravity)
This means you are changing something for all the instances of the class???
Is this really what you want to do?
Not seeing all the code make difficult to undestand what is your pb exactly.
Ship = class(Thing)
function Ship:init()
Thing.init(self) -- is correct
Thing:init() -- is equivalent to Thing.init(Thing) so it changes the table Thing, not the table self, which is what you really want to do. Got it?
...
end