Does ++ return a value in EK9? Can I write x <- y++?
← Operators and Expressions · Ref: Q1057
In EK9, ++ and += are mutation operators. They modify the variable in place and return NOTHING. You cannot use them in expressions.
++ INCREMENTS IN PLACE:
count <- 0 count++ modifies count to 1, returns nothing
+= ADDS IN PLACE:
score <- 10 score += 5 modifies score to 15, returns nothing
WHAT YOU CANNOT DO:
x <- count++ WRONG — ++ returns nothing, cannot assign total <- a += b WRONG — += returns nothing, cannot assign if count++ > 5 WRONG — ++ returns nothing, cannot compare
WHAT TO DO INSTEAD:
count++ x <- count increment then read separately
All mutation operators return void: ++, --, +=, -=, *=, /=, :=:, :^:, :~:
Mutation operators CANNOT be marked 'as pure' because they change state. A pure function must not mutate.
See Q241 for all mutation operators. See Q908 for $ operator. See Q1055 for default vs manual operators.
Example
defines module qa.operators.incrementmutator defines program IncrementDemo() stdout <- Stdout() count <- 0 stdout.println(`Before: ${count}`) //++ mutates in place, returns nothing count++ stdout.println(`After ++: ${count}`) //+= mutates in place, returns nothing count += 10 stdout.println(`After += 10: ${count}`) //-= also mutates in place count -= 3 stdout.println(`After -= 3: ${count}`)
Common mistakes
E07950 — ++ mutates in place and returns nothing, so it cannot appear inside an expression such as string interpolation; increment first, then read the variable. See ek9 -h E07950 for details.
Incorrect:
Before: ${count++}
Correct:
Before: ${count}
Other ways to ask this
- Is ++ a mutator or does it return a new value?
- How do ++ and += work in EK9?
- Can I use ++ in an expression in EK9?
Coming from another language?
Java/C/Go: ++ returns a value (pre/post increment). Python: no ++ operator. EK9: ++ mutates in place, returns nothing — cannot be used in expressions.
Keywords: plus, operator, expression, mutator, void, increment