leiradel wrote:
Yeah, I thought I could implement some things I had in mind using mixins, but when I started to write the code I realized they couldn't do what I wanted (please correct me if I'm wrong). In your example, how could I have different implementations of "fly" for different animals?
Mixins are like interfaces, but better; they come "filled in" with a "default implementation". But you can still change them.
Your code with mixins would look like this.
Code: Select all
Bee = class( Insect ):includes(HasWings) -- This adds a default fly implementation to Bee
function Bee:fly() -- this re-defines fly for Bee
print( 'Bzzz Bzzz Bzzz I am a ' .. self:getClassName() )
end
On this particular case, we're redefining the mixin completely, since it only has one function. It is still useful to include it, since
includes(HasWings, Bee) will return true; this allows making things a bit more semantic later on (
if includes(HasWings, class) then ... ).
If you need more help, let's open another thread, so this one stays on topic.
better fit to my problems (or would it be mindset?)
It's definitively a mindset thing. Coming from Java, you are naturally used to classes being "closed down" and things having to be declared "in advance" (as with interfaces).
Lua's different: No compile-time type checking, & a lots of things done on runtime. It requires a little effort to get used to it, but once you get over the "I'm going to get lots of errors!" feeling, you will realize it is actually very pleasant to use.
Lua doesn't "protect you from yourself" (some people would call that
treating you like an adult) but that's an advantage.