Gotchas

These aren’t bugs: they’re choices Luma makes on purpose. But they can surprise you, especially if you’re coming from another language.

Whole-number division rounds toward zero

print(7 / 2)
print(-7 / 2)
print(-7 % 3)
3
-3
-1

Dividing two ints gives an int, cut toward zero, and % takes the sign of the left side. For a fraction, use floats: 7.0 / 2.0 is 3.5.

Floats print as what they are

print(0.1 + 0.2)
print(2.0)
0.30000000000000004
2.0

A float is a binary number, and 0.1 + 0.2 isn’t exactly 0.3. Luma prints the shortest text that reads back as exactly the same number, and a whole float keeps its .0, so you can always tell a float from an int.

Bytes wrap around

b: byte = 250
b = b + 10
print(b)
4

A byte holds 0 to 255, and +, - and * wrap around. ints wrap too, but only past about nine quintillion.

The walk variable is a copy

nums: [int] = [1, 2, 3]
nums.walk(n) {
    n = n * 2
}
print(nums)
[1, 2, 3]

n holds the current element; changing it doesn’t change the list. To change the list, use the position: nums.walk(i, n) { nums[i] = n * 2 }, or make a new list with map.

Lists and maps are shared

a: [int] = [1, 2]
b: [int] = a
b[] = 3
print(a)
[1, 2, 3]

Assigning a list, map or set doesn’t copy it: both names refer to the same list. The same goes for passing one to a function, which is why a function can fill a list you give it.

Strings have no [i] and no +

s[0] and "a" + "b" are compile errors. Use s.part(0, 1) for a piece of text, and interpolation ("${a}${b}") or join to build text. See Working with text.

Nothing converts by itself

int + float, int + byte and str into an int are all compile errors. It feels strict at first, but it means a number never silently loses its fraction or turns into text. Use convert when you mean it.

Objects in a circle aren’t freed

Luma frees memory as soon as nothing uses it, by counting references. Two objects that refer to each other keep each other alive. Break the circle (node.children = []) before letting go of them. See Memory.