Dot and Colon Syntax in Lua
Lua’s colon syntax is shorthand for passing the receiver as the first argument, conventionally named self.
Equivalent Method Definitions and Calls
These definitions are equivalent:
1 | function MainScene:onCreate() |
These calls are also equivalent:
1 | scene:onCreate() |
The definition style and call style must agree. If a method is defined with a colon but called with a dot and no explicit receiver, self is missing. If a plain function is called with a colon, it receives one unexpected extra argument.
1 | local Calculator = {} |
Use dot syntax for functions that do not operate on a particular object, and colon syntax for instance methods. The colon does not create a class or copy a function; it only adds the receiver argument at the call or definition site.
When saving a method as a callback, preserve its receiver explicitly:
1 | local callback = function(...) |
Passing scene.onCreate by itself loses the object unless the callback API supplies it separately.