Tag: OOP

Lua Metatables Tutorial

Posted on June 30, 2011 in Tutorials

In this tutorial I'll be covering a very important concept in Lua: metatables. Knowledge of how to use metatables will allow you to be much more powerful in your use of Lua. Every table can have a metatable attached to it. A metatable is a table which, with some certain keys set, can change the behaviour of the table it's attached to. Let's see an example. t = {} -- our normal table mt =...

Read more...

Specifying Names in Lua Without Strings

Posted on April 18, 2011 in Tutorials

Lua is the most flexible language I've ever used, aside from its speed, this is probably the best thing about it. In this post, I'm going to show you how to specify names of things in function arguments, without strings. The method I'll use it a bit hackish, and isn't fit for normal operation; but hey, Lua allows us do it! For an example, say you had an OOP implementation that used a function called...

Read more...

Implementing Proper Getter/Setters in Lua

Posted on April 04, 2011 in Tutorials

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,...

Read more...

ClosureClass

Posted on March 13, 2011 in Projects

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.

Read more...

MiddleClass Extensions

Posted on March 11, 2011 in Projects

I've just recently opened up a new repository at GitHub called middleclass-extensions, which contains my personal hacks, extensions, extras, whatever you want to call them, to MiddleClass. It's a bit like middleclass-extras. So far the repo only contains one extensions, called Static, but this will grow over time. Static is about creating class only methods. When you create a method which you intend to be accessed only via the class, MiddleClass has no way to...

Read more...

Mixin Inheritance in MiddleClass

Posted on March 09, 2011 in Tutorials

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 class:include(ParentMixin) end end 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...

Read more...

Singleton Class in MiddleClass (Lua)

Posted on March 07, 2011 in Tutorials

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...

Read more...