Lua Classes

Lua and classes are a little awkward, as there really is no such thing as classes in Lua. However, it is possible to demonstrate a class like behavior using tables.

Tables are a type of object, and since an instance of a class is also an object, we can use tables as if the language itself supported classes.

You can see that an object called “Triangle” is created and useful functions for it is also created. If we were to execute these functions:

Then the outputs become:

Note that instead of the period ‘.’, I used the colon ‘:’ to create the functions. This is to pass ‘self’ as the first parameter for object-oriented programming. In the case that I used a period to create the functions, the following commands would result in an error.

In the entire reference manual, the colon syntax is mentioned very briefly, but personally, I find this to be a key syntax to effectively conduct OOP using Lua.