Tag: middleclass

Implementing Proper Getter/Setters in Lua

When I create a class in Lua, there are always times when I need to use a getter or setter on attributes, instead of raw access. The way I've always done this is to use methods with names like getFoo and setFoo. And then to keep my API consistent, I have to switch every single property to use these getter/setter methods. The pain about these type of methods is that:

  • You have to switch everything, even attributes that don't need getter and setters, to keep your API consistent. This would end up slowing everything down.
  • It doesn't look as proper as using real getting and setting syntax, like obj.foo and obj.foo = v.
  • It's more to type.

What I really wanted was syntax like this:

Read more


I've recently created an experimental Lua object-orientation library using the closure approach to OOP. It's based largely on MiddleClass, sharing a lot of design features with it.

I've already written a README, so go and visit the GitHub repo for more information.

Mixin Inheritance in MiddleClass

The technique I'm about to present, may seem obvious, but I'll share it anyway. The way I would create mixins that inherit stuff from other mixins is this:

Mixin = {}

function Mixin:included(class)
  if not includes(ParentMixin, class) then

When the mixin is included, it will check whether the class includes the mixin, and if not, it will include it, therefore simulating inheritance. It's good to check if the class includes the parent mixin, because MiddleClass does not do this itself.

Read more

Singleton Class in MiddleClass (Lua)

I thought I'd demonstrate a method for making a singleton class in MiddleClass. If you don't know, MiddleClass is an object-orientation library for Lua.

So why would you want to make a class that only has one instance with MiddleClass? Couldn't you just use a table? Well, making it a class in MiddleClass allows you to take advantage of a number of other cool things that MiddleClass has on offer, like mixins, inheritance and so on. So let's have a look at the method.

Read more