Object-Oriented Programming in Lua With Middleclass

Lua does not have a built-in system for object-oriented programming (OOP), creating classes, instantiating objects, et cetera. And so there are a lot of options for approaching object-oriented programming in Lua. Today I want to introduce you to what has become my favorite library for OOP in Lua: Middleclass by Enrique García, better known by his alias ‘kikito’ in the Lua and LÖVE communities.

A Short Introduction

The project page for Middleclass has a nice example. I want to supplement that with an example of my own. I could have used Middleclass when creating Luvent, like so:

local class = require("middleclass")
local Luvent = class("Luvent")

Now instead of writing the function Luvent.newEvent() I would automatically have a constructor: Luvent:new(), and I could customize its behavior by defining Luvent:initialize(). Middleclass would not require me to change the way I define all of the other methods, and so it easily fits into existing code-bases in my experience.

Middleclass allows you to define the usual metamethods with the exception of __index and __newindex. So if you are using those in existing code you will have to rework it, but again that has not posed a major problem in my experience. Some programmers use those metamethods to implement private data members. Middleclass has no explicit way to mark something as private; the convention is to use a leading underscore when naming private properties. To me that is good enough, but if you want privacy there are ways to do it.

Two other reasons I like Middleclass: easy static properties and mixins. The terse examples on the Middleclass wiki speak for themselves and demonstrate Middleclass’s nice API. The library lets you determine if an object isInstanceOf(class) or isSubclassOf(class), tests that are common in object-oriented languages. And so I believe many programmers who are used to OOP will feel comfortable with Middleclass.

That’s It?

Yes. I realize this article is short, but that is all I have to say about Middleclass. It is, in my opinion, the best library for OOP in Lua. Programmers who prefer the object-oriented paradigm should feel at home using Middleclass. And to top it all off it is easy to introduce into existing code-bases if you decide that an OOP approach would be beneficial.

So try it out. And even if you don’t use it I still suggest looking at the source code, as it’s an example of good Lua code and style.


2 thoughts on “Object-Oriented Programming in Lua With Middleclass

  1. Hi there!

    Thanks for your kind words, I’m glad that you are finding middleclass useful.

    Your post was very instructive, but I’d like to add a small bit if that’s ok. Someone asked me the other day when *not* to use middleclass (and instead use some more basic inheritance based on the __index metamethod, as Programming In Lua indicates).

    I thought about it for a while a came out with several rules of thumb:

    * If you are not creating lots of “instances” (Lua tables that share common functionality) then you don’t need OOP in the first place – be it middleclass-based or __index-based.
    * If you have instances, but you are not having any of these features: class methods & instance methods, subclasses, mixins, metamethods, instanceOf/subclassOf, and you know how to use __index, then you’ll be fine using __index. Otherwise use middleclass.
    * Lua isn’t great at managing library dependencies. If you are doing a library, having middleclass as a dependency will make things a bit more difficult. It’s not a final decision, but it’s a factor you have to balance in your decision, too.
    * Even when you decide to use __index, if you use middleclass’ syntax for defining instance methods you will leave the door open in case you need to add more advanced functionality later on.

    That’s all. Regards!

    1. Thank you for the post, as you bring up some great points. Because Lua comes with no object-oriented system out of the box I think a lot of people rush to find some implementation instead of pausing and considering the possibility that maybe they don’t need OOP in the first place. Or they may be satisified with only the __index-based approach for the reasons you mention.

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s