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.0

There 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
15

Where 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

FromToResult
strint, byte, float, boolthe 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, boolstrthe text print shows: 42, 2.5, true
int, bytefloatthe same number (beyond 2^53, the nearest float)
floatint, bytecut off toward zero: 2.9 gives 2, -2.9 gives -2
int, float, boolbyteonly if it is 0..255
byteintthe same number
boolint, bytetrue is 1, false is 0
int, bytebool1 is true, 0 is false; other numbers are an error
str[byte]the text’s bytes (UTF-8)
[byte]strthe bytes as text
filestrthe file’s path
strfilethe same as file(text)
strjsonthe text read as JSON (see JSON)
strdate, time, datetimethe text read as one: 2024-12-25, 14:30, 2024-12-25 14:30:00 (see Dates and times)
date, time, datetimestrthe text print shows
intdatetimea Unix time (seconds) on the local clock (see Unix time)
datetimeintits Unix time
[byte], strbuffera buffer with those bytes
buffer[byte], strits bytes
jsonint, str, [T], {str: T}, …the value inside, if it is one (see JSON)
anything that can be JSONjsonthe 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
true

Conversions 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.