Grav v1.14.0

Operators

Precedence

From loosest to tightest binding. Operators on the same row bind equally and group in the direction shown.

LevelOperatorsGroups
1cond ? a : bright
2??left
3||left
4&&left
5|left
6^left
7&left
8== != < <= > >=left
9<< >>left
10+ -left
11* / %left
12**right
13as, isleft
14unary - ! ~ ++ -- await, &x, *p, casts (T)xright
15calls f(), f(...xs), f::<T>(), indexing a[i], a[i, j], slices a[lo..hi], members . ?. ->, postfix ++ --, unwrap x!left

Assignment (=, +=, ...) is a statement, not an expression.

Operands and arguments are evaluated left to right: a receiver before its method's arguments, both sides of an operator, array and struct literal elements, new arguments, and conditions alike. In an assignment the target's object and indices come before the value (a[i()] = v() calls i first). &&, ||, ??, ? : and ?. evaluate their right side only when needed, and then in the same left-to-right order. Each operand runs once, ??'s left side included.

Arithmetic

OperatorOperandsResult
a + b, a - b, a * bnumbersthe wider type (float if either is)
a / bintegersinteger division, rounding toward zero; dividing by zero throws ArithmeticError
a / bfloatsIEEE division (inf, nan possible)
a % bintegersremainder, with the sign of a; % 0 throws ArithmeticError
a % bfloatsfloating remainder (fmod)
a ** bnumberspower
-anumbernegation
a + bstringsconcatenation

Mixed integer types promote to the wider type, and to unsigned if either side is unsigned; integers narrower than int compute as int. Integer overflow is not checked: it wraps around (two's complement), so 2147483647 + 1 is -2147483648 as an int. Use long for values that can grow large.

Comparison

OperatorWorks on
==, !=every type: numbers by value, strings by content, structs field by field, fixed arrays element by element, enums by value, data enums by variant and fields, objects by identity (or operator==), any by the value inside, T? against null, T or T?
<, <=, >, >=numbers, strings (byte by byte), enums (declaration order), classes with operator<

Comparing with null is always a reference test (or, for T?, a test for a value); it never calls operator==.

Logical

OperatorMeaning
a && btrue when both are; b is skipped when a is false
a || btrue when either is; b is skipped when a is true
!anot

Operands must be bool.

Bitwise and shifts

&, |, ^ (xor), ~ (complement), <<, >> work on integers. A shift by a constant that is not smaller than the operand's width in bits is a compile error; the result of a shift has the (promoted) type of its left operand.

Assignment

x = e     x += e   x -= e   x *= e   x /= e   x %= e
x &= e    x |= e   x ^= e   x <<= e  x >>= e
x++  x--  ++x  --x

The target can be a variable, a field, an element (a[i], including through operator[]/operator[]=) or a dereferenced pointer. x op= e is x = x op e, with x's container and index evaluated once. Captured variables inside a lambda and const names cannot be assigned.

Null handling

OperatorMeaning
a ?? ba unless it is null, else b. On a T?, the result is a T when b is a T.
a?.m, a?.m(...)the member, or a zero value (0, false, "", null) when a is null
x!the value of a T? (or a non-null reference); throws StateError when null

Types

OperatorMeaning
x as T, (T)x, T(x)conversion; see Types
x is Twhether x is a T: a class or subclass, an interface it implements, a data-enum variant, or what an any holds

Conditional

cond ? a : b evaluates cond, then exactly one of a and b. The result type is the common type of the two branches; ok ? 5 : null is an int?.

Indexing and slicing

FormMeaning
a[i]element i of a fixed array, Array, string (a byte), binary or pointer; bounds-checked except on pointers
a[i, j]a class's operator[] with two keys
a[lo..hi], a[lo..=hi], a[lo..], a[..hi]a slice: a new string, Array, Bytes, or (with constant bounds) fixed array

Members and calls

FormMeaning
a.ma field, method, static member, enum member or namespace member
p->m(*p).m through a pointer
f(args)a call; arguments may be named (name: value) and spread (...xs)
f::<T>(args)a call with explicit type arguments
obj.method without a calla function value bound to obj

Overloadable operators

Classes can define + - * / % == != < <= > >= [] []= as methods. See Operators and overloading.

Edit this page on GitHub