Why does EK9 care about operator placement (prefix vs suffix)?
← Getting Started · Ref: Q794
EK9 has strict rules about operator position. Some operators must come BEFORE the expression (prefix), and a few must come AFTER (suffix). Each works only on types that define it.
PREFIX OPERATORS (must come BEFORE)
- $value converts to String - $$value converts to JSON - #? value hashcode (NOT a length - use 'length' for that) - #< text first element, e.g. first Character of a String (types that define #<, such as String and Bits; List does not) - #> text last element (types that define #>, such as String and Bits) - ~ list reverses a List; ~ on Bits or Boolean negates (Integer has no ~) - not active boolean negation - abs number absolute value - sqrt number square root - length name gets length
Emptiness is not a prefix operator: write 'list is empty' (infix).
SUFFIX OPERATORS (must come AFTER)
- value? checks if value is set (isSet) - number! factorial (an expression, like '?') - counter++ increment, and counter-- decrement (statements only - E07950 inside an expression)
COMMON MISTAKES
text#< WRONG: #< is prefix, use '#< text' value$ WRONG: $ is prefix, use '$value' ?value WRONG: ? is suffix, use 'value?'
See Q25 for the promote operator. See Q39 for tri-state and isSet.
Example
defines module qa.gettingstarted.operatorplacement defines program OperatorPlacementDemo() -> userName as String stdout <- Stdout() nameText <- $userName stdout.println(nameText) nameLength <- length userName stdout.println(`Length: ${nameLength}`) if userName? stdout.println("User name is set")
Common mistakes
E01084 — The '$' operator is a prefix operator and must come BEFORE the expression. Write '$userName' not 'userName$'. See ek9 -h E01084 for details.
Incorrect:
nameText <- userName$
Correct:
nameText <- $userName
E01085 — The '?' operator is a suffix operator and must come AFTER the expression. Write 'userName?' not '?userName'. See ek9 -h E01085 for details.
Incorrect:
if ?userName
Correct:
if userName?
Other ways to ask this
- What triggers E01084 prefix operator wrong position?
- What triggers E01085 suffix operator wrong position?
- Why does 'list$' fail but '$list' works?
Coming from another language?
Java: all operators are infix or prefix (++x, !flag). Python: not is prefix, no suffix operators. Rust: ! is prefix (boolean), ? is suffix (error propagation, similar to EK9). Kotlin: !! is suffix (non-null assertion), ! is prefix. Go: ! is prefix, no suffix operators. EK9: strict prefix/suffix distinction enforced at the grammar level.
Keywords: E01084, E01085, prefix, length, position, string, operator, suffix, isSet