Conversions API

Functions for inspecting and converting between types.

typeof(value)

typeof(value)

Returns a string describing the type of value.

Returns: string — one of the type names listed below.

ValueReturns
null"null"
Booleans"bool"
Numbers (integer & float)"number"
Strings"string"
Lists"list"
Maps"map"
Struct instances"struct"
Struct schemas"struct_schema"
Enum values"enum"
Enum schemas"enum_schema"
Functions & closures"function"
Prompt tags"prompt_tag"
Continuations"continuation"
typeof(42)          // "number"
typeof("hello")     // "string"
typeof(true)        // "bool"
typeof(null)        // "null"
typeof([1, 2, 3])   // "list"
typeof({"a": 1})    // "map"

num(value)

num(value)

Converts a string to a number. Leading and trailing whitespace is trimmed before parsing.

Returns: number

Errors: if value is not a string, is empty, or is not a valid number format.

num("42")        // 42
num("3.14")      // 3.14
num("  -7  ")    // -7
num("1e10")      // 10000000000

str(value)

str(value)

Converts any value to its string representation.

str(42)          // "42"
str(3.14)        // "3.14"
str(true)        // "true"
str(null)        // "null"
str([1, 2, 3])   // "[1, 2, 3]"

str(format, ...)

str(format, ...)

Formats a string using a format template and arguments. Supports up to 25 arguments after the format string.

SpecifierTypeDescription
%sstringInsert a string value
%nnumberInsert a number
%bbooleanInsert true or false
%vanyInsert any value’s string representation
%%Literal % character
str("Hello, %s!", "world")              // "Hello, world!"
str("%s is %n years old", "Alice", 30)  // "Alice is 30 years old"
str("Result: %b", true)                 // "Result: true"
str("Value: %v", [1, 2])               // "Value: [1, 2]"
str("100%%")                            // "100%"

See also: String APIMath API