Object-oriented programming in luau

Technically, the RFC on records on the luau github.

Lua does not have first class support for objects since all neccessary behaviour that objects inherit can be done with tables and metatables, and Lua’s base syntax is meant to be lightweight.

OOP in Lua is more akin to defining struct objects with member functions rather than objects, but it can still behave like OOP.

More specifically in Luau, a lot of people use the __index metamethod method as this is just a common pattern in Lua. This is because Lua always created new closures (internal function thing) when you created an object.

In Luau though, if you define your objects correctly, you dont need this pattern (Catwork only uses metatables to override tostring behaviour), because Luau will reference an existing closure under certain circumstances, instead of creating a new one, though thats too technical to explain here.

There are some micro-ops with having less keys on tables but its so minimal it doesn’t matter, key hashes are tiny compared to closures. It also makes iteration less predictable if the methods are not there.