Reference
Operators Reference
Expression operators, precedence, narrowing behavior, and readable conditions.
Operators Reference
Operators are expression syntax. They should stay readable because Pkl modules are configuration first.
Precedence Ladder
From tightest to loosest:
| Level | Shape | Notes |
|---|---|---|
| access | a.b, a?.b, a[i], f(x) |
member, safe access, subscript, call |
| unary | !x, -x |
boolean negation and numeric negation |
| multiplicative | *, /, % |
numeric expressions |
| additive | +, - |
numeric addition and string-like concatenation |
| comparison | <, <=, >, >= |
comparable values |
| equality | ==, != |
value equality |
| type test | is, as |
narrowing and casts |
| boolean and | && |
conjunction |
| boolean or | ` | |
| coalescing | ?? |
fallback for nullable values |
| conditional | if (...) ... else ... |
explicit branch selection |
Parentheses are recommended whenever a rule is hard to scan.
Arithmetic
workers = 2 + 2limit = workers * 10Keep arithmetic close to the values it derives. Use named intermediate properties when units or precedence are not obvious.
Equality and Comparison
isProd = environment == "prod"tooHigh = port > 65535Comparison operators are used heavily by user-defined numeric predicates.
Boolean Logic
public = enabled && (port == 80 || port == 443)Prefer intermediate properties when boolean expressions grow past one line.
Type Tests
function asPort(x: String | Int): Int = if (x is Int) x else 0is guards participate in union narrowing and make later expressions easier to
type.
Null Coalescing
name = suppliedName ?? "default"Use this for simple fallback values. Use if when the fallback needs more
context.
Access Operators
host = server.hostfirst = ports[0]Member and subscript access should fail loudly when the expected member or element is absent. Use safe access when absence is part of the model rather than an error.