Skip to content
Jérôme Leclercq edited this page Dec 8, 2020 · 1 revision

API: tostring

Description:

Receives a value of any type and converts it to a string in a human-readable format.

Prototype:

tostring(v: any, [extended: boolean]) -> str: string

Parameters:

  1. v: Variadic parameters to print.
  2. extended: Optional parameter to indicate if this function should print table content.

Returns:

  1. str: A stringified version of v.

Remarks:

  1. 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).
  2. Independently from the second parameter, this function will try to call the __tostring metamethod and then the __name metafield.

Example code

-- 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))
Clone this wiki locally