Skip to content

std.convert ​

The type conversion module, currently providing value-to-String conversion.

yaoxiang
use std.convert

Conversion Rules ​

to_string formats according to the runtime form of the value:

TypeOutput FormExample
Voidvoidvoid
Booltrue / falsetrue
Intdecimal42
Floatsee below3.14 / 2.0
Charthe character itselfa
Stringoriginal content (no quotes)hello
List[element, ...][1, 2, 3]
Dict{k: v, ...}{a: 1}
Tuple(element, ...)(1, hello)
Array[element, ...][1, 2]
Rangestart..end1..5 (omitted when step is 1)
Bytesbytes[length]bytes[3]

Integer values of Float will have a decimal place appended (2.0 instead of 2), to distinguish between Int and 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 an arbitrary 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")
}

The string itself has no quotes added:

yaoxiang
use std.assert
use std.convert

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

Compound types are recursively expanded (the format is intentionally not asserted to avoid fragility):

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 Method Form ​

In addition to the general-purpose convert.to_string, the module also exports the following same-named functions bound by type, with behavior identical to it:

Export 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; for everyday code, simply use convert.to_string.

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

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