Converting between types: convert
Luma never converts a value to another type by itself. convert(x) does it when you ask, and the type it converts to is the type the value goes into:
text: str = "42"
n: int = convert(text)
f: float = convert(n)
back: str = convert(f)
print(n + 1)
print(f / 4.0)
print(back)Output:
43
10.5
42.0There is one convert, not to_int, to_float, to_str: the code already says what you want, n: int = ....
Where the type comes from
Wherever the value goes somewhere with a known type: a declaration, an assignment, a function’s argument, a return, a list element or a map key. With an operator, convert takes the type of the other side:
count: int = 10
count = count + convert("5")
if convert("3") > 2 {
print("three is more")
}
print(count)Output:
three is more
15Where nothing says what to convert to, it’s a compile error:
print(convert("5")) // error: nothing says what to convert to here; put the result into
// a variable of the type you want, e.g. n: int = convert(text)What converts to what
| From | To | Result |
|---|---|---|
str | int, byte, float, bool | the text read as that type, like prompt input: spaces around it are ignored, "abc" is an error; a float is the one nearest to the number, to the last bit |
int, byte, float, bool | str | the text print shows: 42, 2.5, true |
int, byte | float | the same number (beyond 2^53, the nearest float) |
float | int, byte | cut off toward zero: 2.9 gives 2, -2.9 gives -2 |
int, float, bool | byte | only if it is 0..255 |
byte | int | the same number |
bool | int, byte | true is 1, false is 0 |
int, byte | bool | 1 is true, 0 is false; other numbers are an error |
str | [byte] | the text’s bytes (UTF-8) |
[byte] | str | the bytes as text |
file | str | the file’s path |
str | file | the same as file(text) |
str | json | the text read as JSON (see JSON) |
str | date, time, datetime | the text read as one: 2024-12-25, 14:30, 2024-12-25 14:30:00 (see Dates and times) |
date, time, datetime | str | the text print shows |
int | datetime | a Unix time (seconds) on the local clock (see Unix time) |
datetime | int | its Unix time |
[byte], str | buffer | a buffer with those bytes |
buffer | [byte], str | its bytes |
json | int, str, [T], {str: T}, … | the value inside, if it is one (see JSON) |
| anything that can be JSON | json | the value as JSON (this also happens by itself where a json is expected); an obj becomes an object of its fields |
price: float = 19.99
euros: int = convert(price)
bits: [byte] = convert("Hi")
word: str = convert(bits)
yes: bool = convert(1)
print(euros)
print(bits)
print(word)
print(yes)Output:
19
[72, 105]
Hi
trueConversions that can never work, like a list to an int, are compile errors: convert can't turn a [int] into an int.
When a value doesn’t fit
Some values can’t be converted: "abc" isn’t a number, 300 doesn’t fit in a byte, 2 isn’t a bool. Then convert raises error 11, which try can catch:
err: error = nil
age: int = 0
try err {
age = convert("forty")
}
print(err.message)
try err {
small: byte = convert(300)
}
print(err.message)Output:
cannot convert "forty" to an int
cannot convert 300 to a byte (0..255)convert never changes a value silently to make it fit: 300 doesn’t wrap around to 44, and nan or 1e30 don’t become some int.