Middleclass 4.x Object-Orientation for Lua

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [Lib] Middleclass 4.0.0. Object-Orientation for Lua

Post by kikito »

Zireael wrote: Tue Feb 21, 2017 11:48 am I just realized I am using the outdated Lua module keyword in my project. :oops:

I am looking for an OOP implementation that allows multiple inheritance. I looked through git and github wiki and I am not sure whether middleclass supports it or not.
Well, it does support a certain kind of multiple inheritance. It might not be what you are looking for though.

In middleclass, every class has a single superclass (or none at all). So in that regard there is no multiple inheritance.

However, middleclass allows sharing behaviour through mixins. This behaviour was inspired by Ruby.

Mixins are basically groups of methods that you can group together and share between several classes. On that regard that works like multiple inheritance. Mixins just don't come with a constructor. This is how middleclass sidesteps the Diamond Problem. You (the middleclass user) are responsible of explicitly building your class instances so that they work correctly with the mixins you include in them.

I recommend you to think hard about using composition instead of inheritance. In the long run, it is usually better to have a physics_body attribute which is an instance of PhysicsBody and a health attribute which is an instance of Health, instead of inheriting directly from both PhysicsBody and Health.
When I write def I mean function.
User avatar
Forgemistress
Prole
Posts: 9
Joined: Thu Dec 01, 2016 1:50 am

Re: Middleclass 4.x Object-Orientation for Lua

Post by Forgemistress »

That aside, multiple inheritance is a pox on object oriented programming to begin with. Used sparingly, it can most certainly be useful in lower level languages like C++. However mixin functionality that defines common sets of functions and behaviors given a dataset is more or less what you'd want to use instead. After implementing my own class definition library and knowing what's going on under the hood, it just doesn't seem like this kind of behavior would be useful.
The flame cleanses all. It clears away the old to make way for the new. Forge for yourself new opportunities from the dross of the old.
Post Reply

Who is online

Users browsing this forum: No registered users and 172 guests