Menu
Coddy logo textTech

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)
end

The colon also works when calling the function. It automatically passes the table as the first argument:

player:greet()  -- Output: Hello, I am Hero

This 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 icon

Challenge

Easy

Create 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:

  1. The greeting message to store in greeter.message
  2. 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)
end

When 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 greeting
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming