-
-
Notifications
You must be signed in to change notification settings - Fork 9
tostring
Jérôme Leclercq edited this page Dec 8, 2020
·
1 revision
API: tostring
Receives a value of any type and converts it to a string in a human-readable format.
tostring
(v: any
, [extended: boolean
]) -> str: string
-
v
: Variadic parameters to print. -
extended
: Optional parameter to indicate if this function should print table content.
-
str
: A stringified version ofv
.
- This function is from the Lua standard (see print in the official Lua manual), although it has been extended with a second parameter (if no value or false is passed as the second parameter this function behaves like the original
tostring
function). - Independently from the second parameter, this function will try to call the
__tostring
metamethod and then the__name
metafield.
-- Prints "Hello world" to the output
print(tostring("Hello world"))
-- Prints "true 1.24 str vec2(10, 20)" to the output
print(tostring(true), tostring(1.24), tostring("str"), tostring(Vec2(10, 20)))
-- Prints the table pointer (something like "table: 0x7f3fbd713598")
print(tostring({ "Hello", "World", key = "value" }))
--[[
Prints the table content:
{
1 = Hello
2 = World
key = value
}
]]
print(tostring({ "Hello", "World", key = "value" }, true))