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 tableWhen 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 expectedWhile 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
EasyCreate a table named player with a field score initialized to 0.
Add two functions to the table:
player.setScore(self, value)- defined with dot syntax and explicitselfparameter. Setsself.scoreto the given value.player:addBonus(amount)- defined with colon syntax. Addsamounttoself.score.
You will receive two inputs:
- An initial score value
- A bonus amount to add
Perform the following steps:
- Call
setScoreusing dot syntax (passing the table as the first argument) to set the initial score - Call
addBonususing colon syntax to add the bonus - 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 explicitself, 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 tableIncorrect 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 issuesCorrect 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)This 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