Quirrel4.41.0

types.String.constructor

Method on every string
constructor(value): any

Converts value to a string. This is what runs when types.String itself is called, types.String(value), the same way types.Integer(value) converts to an integer.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

valueanythe value to convert

Return value

value converted to a string, the same conversion tostring(value) performs for the built-in types (a number prints its digits, true/false print as those words, null prints as "null", and a table, array, class, instance, function, generator, thread or weakref prints a description such as "(table : 0x...)" unless its class defines _tostring).

Errors

Throws cannot convert to String when the conversion fails. Every built-in type converts successfully, so this is not reachable through ordinary Quirrel code; it exists for a _tostring metamethod that itself fails.

Notes

Takes exactly one argument: calling types.String() with no argument, or with two or more, throws a wrong-number-of-parameters error, because this binding carries no declaration string and so cannot show value by name; the VM dumps it as (table|userdata|instance|class|null).constructor(arg1), a generic fallback receiver used for every binding with no declared parameter types, not the real receiver.

constructor is a reserved word (it also introduces a class's constructor method), but the parser special-cases it after a dot, so x.constructor reads like any other field access. types.String.clone has no such exception; see its Notes.

Example

examples/types/string/constructor.nut
let types = require("types")

// types.String(value) is what runs when you call the class directly.
println("types.String(123) =", types.String(123))
println("types.String(true) =", types.String(true))
println("types.String(null) =", types.String(null))

// exactly one argument: none, or more than one, is an error.
try { types.String() } catch (e) println("types.String() throws:", e)
Output:
types.String(123) = 123
types.String(true) = true
types.String(null) = null
types.String() throws: wrong number of parameters passed to native closure 'constructor' (1 passed, 2 required)

See also

tostringReturns str itself.
classofReturns the class that obj belongs to.
stringclass index