The __tostring Metamethod
Part of the Logic & Flow section of Coddy's Lua journey — lesson 20 of 54.
When you print a table in Lua, you typically see something unhelpful like table: 0x5f8a20—just a memory address. The __tostring metamethod lets you change this behavior by defining exactly what should be displayed when your table is converted to a string.
This metamethod is particularly useful for debugging and creating cleaner output. When Lua needs to convert your table to a string (like when using print()), it checks if the table has a metatable with a __tostring field. If it does, Lua calls that function and uses its return value instead of the default representation.
Here's how to implement it:
local vector = {x = 3, y = 4}
local meta = {
__tostring = function(t)
return "Vector(" .. t.x .. ", " .. t.y .. ")"
end
}
setmetatable(vector, meta)
print(vector) -- Output: Vector(3, 4)The __tostring function receives the table as its argument and must return a string. In this example, it constructs a formatted string showing the vector's x and y values in a readable format.
This makes your code much easier to work with. Instead of seeing cryptic memory addresses during debugging, you get meaningful information about what your table contains.
Challenge
EasyWrite a function createPoint that takes x and y coordinates and returns a point table with a custom string representation.
Create a table with x and y fields, attach a metatable with the __tostring metamethod, and return the result of converting the table to a string using tostring().
Logic:
- Create a table with keys
xandyset to the provided coordinates - Create a metatable with
__tostringthat returns a string in the format"Point(x, y)" - Attach the metatable to the point table using
setmetatable() - Return
tostring(point)to get the custom string representation
Parameters:
x(number): The x-coordinate of the pointy(number): The y-coordinate of the point
Returns: The custom string representation of the point in the format "Point(x, y)" (string)
Cheat sheet
The __tostring metamethod allows you to customize how a table is displayed when converted to a string. By default, printing a table shows a memory address like table: 0x5f8a20.
When Lua converts a table to a string (such as with print()), it checks if the table has a metatable with a __tostring field. If present, Lua calls that function and uses its return value.
The __tostring function receives the table as its argument and must return a string:
local vector = {x = 3, y = 4}
local meta = {
__tostring = function(t)
return "Vector(" .. t.x .. ", " .. t.y .. ")"
end
}
setmetatable(vector, meta)
print(vector) -- Output: Vector(3, 4)You can also explicitly convert a table to a string using tostring(), which will invoke the __tostring metamethod if defined.
Try it yourself
function createPoint(x, y)
-- Write code here
end
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Table Iteration
Iterating with pairs()Iterating with ipairs()pairs() vs. ipairs()Recap - Character Sheet4Introduction to Metatables
What is a Metatable?setmetatable & getmetatableThe __index MetamethodThe __newindex MetamethodThe __tostring MetamethodArithmetic Metamethods Part 1Arithmetic Metamethods Part 2Recap - Read-Only Table2More Table Library Functions
table.concat()table construction & unpack()table.sort()Custom Sorting with FunctionsRecap - High Score Board