Skip to content

std.convert ​

The type conversion module currently provides conversion of values to String.

yaoxiang
use std.convert

Conversion Rules ​

to_string formats a value according to its runtime form:

TypeOutput FormExample
Voidvoidvoid
Booltrue / falsetrue
IntDecimal42
FloatSee below3.14 / 2.0
CharThe character itselfa
StringOriginal content (without quotes)hello
List[element, ...][1, 2, 3]
Dict{k: v, ...}{a: 1}
Tuple(element, ...)(1, hello)
Array[element, ...][1, 2]
Rangestart..end1..5 (the step is omitted when it is 1)
Bytesbytes[length]bytes[3]

Integral values of Float are given a decimal place (2.0 rather than 2) to distinguish Int from Float.

Function Overview ​

FunctionSignature
to_string(value) -> String
int.to_string(self) -> String
float.to_string(self) -> String
bool.to_string(self) -> String
char.to_string(self) -> String
string.to_string(self) -> String
list.to_string(self) -> String
dict.to_string(self) -> String
tuple.to_string(self) -> String
set.to_string(self) -> String
range.to_string(self) -> String

Functions ​

to_string ​

yaoxiang
to_string: (value) -> String

Converts any value to its string representation.

  • value — a value of any type

Returns: the formatted string. Returns "()" when the argument is missing.

yaoxiang
use std.assert
use std.convert

main: () -> Void = {
    assert(convert.to_string(42) == "42")
    assert(convert.to_string(true) == "true")
    assert(convert.to_string(false) == "false")
}

Strings themselves are not quoted:

yaoxiang
use std.assert
use std.convert

main: () -> Void = {
    assert(convert.to_string("hi") == "hi")
}

Composite types are expanded recursively (the format is not asserted, to avoid brittleness):

yaoxiang
use std.assert
use std.convert
use std.string

main: () -> Void = {
    s_list = convert.to_string([1, 2, 3])
    assert(string.len(s_list) > 0)

    s_dict = convert.to_string({})
    assert(string.len(s_dict) > 0)
}

Type-Specific Method Form ​

In addition to the general-purpose convert.to_string, the module exports the following type-bound functions of the same name, whose behavior is identical:

Exported NameSignature
int.to_string(self) -> String
float.to_string(self) -> String
bool.to_string(self) -> String
char.to_string(self) -> String
string.to_string(self) -> String
list.to_string(self) -> String
dict.to_string(self) -> String
tuple.to_string(self) -> String
set.to_string(self) -> String
range.to_string(self) -> String

These bindings are used for runtime Stringable dispatch; in ordinary code, you can simply use convert.to_string.

The Set type has been removed from the language layer; set.to_string is retained as a compatibility placeholder.

  • std.io — the internal formatting of print / println follows the same set of rules
  • std.string — string operations