Operators

Arithmetic

OperatorMeaningWorks on
+addint, float, byte
-subtractint, float, byte
*multiplyint, float, byte
/divideint, float, byte
%remainderint, byte
-xnegateint, float, byte

Both sides must have the same type (see Types don’t mix).

print(7 + 3)
print(7 - 10)
print(6 * 7)
print(7 / 2)
print(7 % 3)
print(7.0 / 2.0)
print(-(2 + 3))

Output:

10
-3
42
3
1
3.5
-5

Whole-number division

For int and byte:

  • / drops the fraction, rounding toward zero: 7 / 2 is 3, -7 / 2 is -3.
  • % gives the remainder, with the sign of the left side: 7 % 3 is 1, -7 % 3 is -1.
  • Dividing by zero (with / or %) stops the program with a runtime error:
zero: int = 0
print(10 / zero)

Output:

example.luma:2:10: runtime error: division by zero

For floats, division by zero gives inf or nan instead (see float).

Overflow

int results wrap around at 64 bits, and byte results wrap around within 0 to 255. Floats follow IEEE 754 and become inf when they’re too large.

Comparison

OperatorMeaningWorks on
==equalall types except lists, maps and sets
!=not equalall types except lists, maps and sets
<less thanint, float, byte, str
<=less than or equalint, float, byte, str
>greater thanint, float, byte, str
>=greater than or equalint, float, byte, str

A comparison gives a bool.

print(3 < 5)
print(3 == 5)
print("abc" == "abc")
print("abc" != "abd")
print("Kask" < "Tamm")
print(true == false)

Output:

true
false
true
true
true
false
  • Strings are equal when they contain exactly the same characters.
  • Strings are ordered by their characters’ numbers (code points), one after another, as sort() puts them: "Apple" < "apple", "z" < "ä", and "ab" < "abc" (a string comes before the longer ones it starts).
  • Comparisons can’t be chained: 1 < x < 10 is an error. Write 1 < x && x < 10.

Logic

OperatorMeaning
a && btrue if both are true
a || btrue if at least one is true
!atrue if a is false

The operands must be bool. There is no “truthiness”: if count is an error; write if count > 0.

age: int = 30
print(age >= 18 && age <= 65)
print(age < 18 || age > 65)
print(!(age == 30))

Output:

true
false
false

Short-circuit evaluation

&& and || only evaluate the right side when it can change the result:

  • a && b doesn’t evaluate b when a is false.
  • a || b doesn’t evaluate b when a is true.

This makes guards like the following safe:

zero: int = 0
if zero != 0 && 10 / zero > 1 {
    print("never")
}
print("no division by zero happened")

Output:

no division by zero happened

Bitwise

Bitwise operators work on the bits of an int or a byte. Network protocols, file formats and flags need them.

OperatorMeaningWorks on
a & band: the bits that are 1 in bothint, byte
a | bor: the bits that are 1 in eitherint, byte
a ^ bxor: the bits that are 1 in one of them, not bothint, byte
a << nshift left by n bitsint, byte
a >> nshift right by n bitsint, byte
print(12 & 10)
print(12 | 10)
print(12 ^ 10)
print(1 << 10)
print(0xFF00 >> 8)

Output:

8
14
6
1024
255

For &, | and ^, both sides have the same type, as with arithmetic. They don’t work on bool: for those, use && and ||.

Shifts

In a << n and a >> n, the left side decides the type, and n is an int (or a byte):

  • << moves the bits up and fills with 0. On a byte, bits moved past the 8th are gone: x << 1 with x = 200 is 144.
  • >> moves the bits down and keeps the sign of an int: -16 >> 2 is -4.
  • Shifting by 64 or more gives 0, and -1 for >> of a negative int. On a byte, shifting by 8 or more gives 0.
  • Shifting by a negative number raises error 1: cannot shift by -1.

There is no “not” operator for bits. x ^ -1 flips all the bits of an int, and b ^ 255 flips all the bits of a byte.

Flags

A common use is a set of yes/no flags packed into one number:

lock READ: int = 1 << 0
lock WRITE: int = 1 << 1
lock ADMIN: int = 1 << 2

rights: int = READ | WRITE
print(rights & WRITE != 0)
print(rights & ADMIN != 0)
rights = rights | ADMIN       // turn ADMIN on
rights = rights & (WRITE ^ -1)  // turn WRITE off
print(rights)

Output:

true
false
5

And reading a number from bytes, lowest byte first:

data: [byte] = [0x2C, 0x01, 0x00]
size: int = convert(data[0]) | convert(data[1]) << 8 | convert(data[2]) << 16
print(size)

Output:

300

A buffer does this for you with b.read(3); bitwise operators are for what is packed inside the numbers.

Precedence

From highest (done first) to lowest:

LevelOperators
1-x !x
2* / % & << >>
3+ - | ^
4?? (a default for an optional value: nick ?? "anonymous")
5== != < <= > >= is (which obj a trait value holds, what a json value is)
6&&
7||

Operators on the same level are evaluated from left to right: 10 - 4 - 3 is (10 - 4) - 3. (?? goes the other way: a ?? b ?? c is a ?? (b ?? c), the first that isn’t nil). Use parentheses to change the order.

The bitwise operators bind tighter than comparisons, so flags & WRITE != 0 means (flags & WRITE) != 0. A shift is like a multiplication, so 1 + 2 << 3 is 1 + (2 << 3), which is 17.

print(1 + 2 * 3)
print((1 + 2) * 3)
print(10 - 4 - 3)
print(true || false && false)

Output:

7
9
3
true