Menu
Coddy logo textTech

Dot vs Colon

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 4 of 70.

Now that you know both the dot and colon syntax, it's crucial to understand when to use each—and what happens when you mix them incorrectly.

The key rule is simple: how you define a function must match how you call it. If you define with a colon, call with a colon. If you define with a dot and explicit self, call with a dot and pass the table.

Here's what happens when you mismatch them:

local game = { score = 0 }

function game:addPoints(points)
    self.score = self.score + points
end

-- WRONG: calling with dot, but no table passed
game.addPoints(10)  -- Error! self is now 10, not the table

When you call game.addPoints(10), Lua doesn't automatically pass game. The value 10 becomes self, causing unexpected behavior or errors.

The reverse mistake also causes problems:

function game.reset(self)
    self.score = 0
end

-- WRONG: calling with colon passes game twice
game:reset()  -- self receives game, but an extra nil argument is also expected

While this particular case might work, it creates confusion and can break functions that expect specific arguments. The safest approach is to stay consistent: use colon for both definition and call when the function needs self, and use dot when it doesn't.

challenge icon

Challenge

Easy

Create a table named player with a field score initialized to 0.

Add two functions to the table:

  1. player.setScore(self, value) - defined with dot syntax and explicit self parameter. Sets self.score to the given value.
  2. player:addBonus(amount) - defined with colon syntax. Adds amount to self.score.

You will receive two inputs:

  1. An initial score value
  2. A bonus amount to add

Perform the following steps:

  1. Call setScore using dot syntax (passing the table as the first argument) to set the initial score
  2. Call addBonus using colon syntax to add the bonus
  3. Print the final value of player.score

Remember: match how you call each function to how it was defined.

Cheat sheet

When defining and calling methods in Lua, the syntax must match:

  • If you define with a colon (:), call with a colon
  • If you define with a dot (.) and explicit self, call with a dot and pass the table

Incorrect usage - calling with dot when defined with colon:

function game:addPoints(points)
    self.score = self.score + points
end

game.addPoints(10)  -- Error! self becomes 10, not the table

Incorrect usage - calling with colon when defined with dot:

function game.reset(self)
    self.score = 0
end

game:reset()  -- Confusing: passes game automatically but may cause issues

Correct usage - matching syntax:

-- Define with colon, call with colon
function game:addPoints(points)
    self.score = self.score + points
end
game:addPoints(10)

-- Define with dot, call with dot
function game.reset(self)
    self.score = 0
end
game.reset(game)

Try it yourself

-- Read input
local initialScore = tonumber(io.read())
local bonusAmount = tonumber(io.read())

-- TODO: Write your code below
-- 1. Create a table named 'player' with a field 'score' initialized to 0
-- 2. Add player.setScore(self, value) using dot syntax with explicit self
-- 3. Add player:addBonus(amount) using colon syntax
-- 4. Call setScore using dot syntax (passing table as first argument)
-- 5. Call addBonus using colon syntax


-- Output the final score
print(player.score)
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