The Colon Syntax
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 3 of 70.
Writing counter.increment(counter) every time is tedious and error-prone. Lua provides a cleaner way: the colon (:) operator.
When you define a function using a colon, Lua automatically adds self as the first parameter:
local player = {
name = "Hero"
}
-- Using colon in the definition
function player:greet()
print("Hello, I am " .. self.name)
endThe colon also works when calling the function. It automatically passes the table as the first argument:
player:greet() -- Output: Hello, I am HeroThis is equivalent to writing player.greet(player), but much cleaner. The colon handles the self parameter on both ends—during definition and during the call.
Here's a comparison showing both approaches producing identical results:
local counter = { value = 0 }
-- Dot syntax (explicit self)
function counter.increment(self)
self.value = self.value + 1
end
counter.increment(counter)
-- Colon syntax (automatic self)
function counter:decrement()
self.value = self.value - 1
end
counter:decrement()The colon syntax is the standard way to define and call methods in Lua. It makes your code shorter and less repetitive.
Challenge
EasyCreate a table named greeter with a field message initialized to a value you'll receive as input.
Add a method :sayHello(name) using the colon syntax that prints the greeting in this format:
[message], [name]!You will receive two inputs:
- The greeting message to store in
greeter.message - A name to pass to the
:sayHello()method
Define the method using the colon syntax and call it using the colon syntax. Print the formatted greeting.
For example, if the message is Welcome and the name is Luna, the output should be:
Welcome, Luna!Cheat sheet
The colon (:) operator provides a cleaner syntax for defining and calling methods in Lua by automatically handling the self parameter.
When defining a function with a colon, self is automatically added as the first parameter:
function player:greet()
print("Hello, I am " .. self.name)
endWhen calling a function with a colon, the table is automatically passed as the first argument:
player:greet() -- Equivalent to player.greet(player)Comparison of dot syntax vs colon syntax:
-- Dot syntax (explicit self)
function counter.increment(self)
self.value = self.value + 1
end
counter.increment(counter)
-- Colon syntax (automatic self)
function counter:decrement()
self.value = self.value - 1
end
counter:decrement()The colon syntax is the standard way to define and call methods in Lua, making code shorter and less repetitive.
Try it yourself
-- Read input
local message = io.read()
local name = io.read()
-- TODO: Write your code below
-- 1. Create a table named 'greeter' with a field 'message'
-- 2. Add a method :sayHello(name) using colon syntax
-- 3. Call the method using colon syntax to print the greetingThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1The 'Self' Concept
Tables with FunctionsExplicit 'self'The Colon SyntaxDot vs ColonRecap - Moving Point4Project: Digital Bank
Project SetupDeposit Method7Polymorphism & Overriding
Overriding MethodsCalling Parent MethodsDuck TypingCommon InterfaceChecking TypeRecap - Employee Roles10Project: Shape Manager
Project SetupRectangle Class2Class Prototype Pattern
The Prototype ConceptLinking with __indexThe :new() ConstructorInitializing AttributesIndependent InstancesRecap - Car Factory5Operator Overloading in OOP
Adding ObjectsSubtracting ObjectsConcatenating ObjectsComparing Objects (<, >)Recap - Wallet Math8Encapsulation
Naming ConventionsClosures for PrivacyAccess via ClosuresRead-Only TablesValidation LogicRecap - Secure Vault11Design Patterns (Lite)
Factory FunctionsSingleton TableIterator PatternObserver (Listener)Recap - Logger Factory3Object State and Behavior
Instance VariablesGetter MethodsSetter MethodsCalculated PropertiesFormatting StringsEquality ChecksRecap - Student Grade6Inheritance Basics
The Inheritance SetupInheriting MethodsExtending the ConstructorAdding Child MethodsShared vs UniqueRecap - Shape Hierarchy