If you're reading this document, you're probably one of many people confused about how to implement OOP-like classes in Lua. But no worries! This document aims to explain all the fundamental concepts in beginner-friendly language.
Before we start, we need to talk about metatables. These are Lua's way of allowing users to overload operators. Operators include
arithmetic +
, -
, *
, /
, etc., but also things like indexing tables a[b]
, creating new indices in tables a[b] = c
, function
calls, a(b, c, d)
, you get the idea.
We can set the metatable of a table using setmetatable(t, metatable)
. The metatable
is another table, that contains fields for overriding