Contents

Operators

Arithmetic Operators

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
**Exponentiation
%Modulus (remainder)

Division follows the type of the operands: dividing two int values gives an int (truncated toward zero), and dividing decimal values gives a decimal (7 / 2.0 is 3.5); an int operand takes the type of the other one. A decimal and a float do not mix: one is converted with decimal(x) or float(x). The remainder % has the sign of the dividend.

7 / 2 // 3
-7 / 2 // -3
7.0 / 2.0 // 3.5
7 / 2.0 // 3.5
-7 % 2 // -1

// is always a comment (see Comments).

Operators cannot return a Result, so an invalid arithmetic operation panics:

  • division by zero (1 / 0, 1.5 % 0.0, 0.0 ** -1.0);
  • results that do not fit in an int, a decimal or a float (overflow);
  • ** with a result that is not a real number ((-8.0) ** 0.5).

0 ** 0 is 1.

To handle the ** cases instead of panicking, use math.pow, which returns a MathError (Complex or Infinite; see math).

Comparison Operators

OperatorDescription
==Equality
!=Inequality
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
isVariant check (whether a value is a given variant)
inMembership check (whether an element is in a collection)

== compares values: two lists or objects are equal when their contents are equal. Values that hold functions or move-only values (such as files) cannot be compared (see Constraints).

type Person = {
    name: string
    age: int
}

immut a: Person = { name: 'Ana', age: 30 }
immut b: Person = { name: 'Ana', age: 30 }

a == b // true: the fields are equal

in checks whether an element is in a list, or a text in a string:

'a' in 'banana' // true
2 in [1, 2, 3] // true

is checks the variant of a sum type value. Inside the if, the fields of the variant can be used:

immut result = Some(1)

if (result is Some) {
    print(result.value) // 1
}

Logical Operators

OperatorDescription
andLogical AND
orLogical OR
notLogical NOT
if (age >= 18 and not hasTicket) {
    print('buy a ticket first')
}

Assignment Operators

OperatorDescription
=Assignment (assigns a value to a variable)

Compound Operators

OperatorDescriptionEquivalent
+=Add and assignx = x + y
-=Subtract and assignx = x - y
*=Multiply and assignx = x * y
/=Divide and assignx = x / y
**=Exponentiate and assignx = x ** y
%=Modulus and assignx = x % y
&=AND and assignx = x & y
|=OR and assignx = x | y
^=XOR and assignx = x ^ y
<<=Shift left and assignx = x << y
>>=Shift right and assignx = x >> y

String Operators

OperatorDescription
+Concatenation (joins strings)
*Repetition (repeats a string multiple times)

Bitwise Operators

Bitwise operators operate on the binary representations of integers.

OperatorDescriptionExample (a = 5, b = 3)
&ANDa & b → 1
|ORa | b → 7
^XORa ^ b → 6
~NOT~a → -6
<<Shift lefta << 1 → 10
>>Shift righta >> 1 → 2

Explanation:

  • a = 5 → 0101 (binary), b = 3 → 0011 (binary)
  • a & b → 0101 & 0011 = 0001 → 1
  • a | b → 0101 | 0011 = 0111 → 7
  • a ^ b → 0101 ^ 0011 = 0110 → 6
  • ~a → bitwise NOT of 0101 = 1010 (in two's complement, this is -6)
  • a << 1 → shift 0101 one bit to the left = 1010 → 10
  • a >> 1 → shift 0101 one bit to the right = 0010 → 2

Edit this page on GitHub