Why are ++ and -- statement-only operators in EK9?

← Security and Sanitization · Ref: Q664

In EK9, '++' and '--' are STATEMENT-ONLY operators (E07950). They cannot be used in expressions where a value is expected.

WHY REMOVED FROM EXPRESSIONS

In C/Java, 'y <- x++' has confusing semantics:

  C: y gets the OLD value before increment (surprising!)
  Java: y becomes an ALIAS to x (both reference same object!)

Both are sources of subtle bugs.

CORRECT PATTERN

  x++                    // Statement only: increments x
  y <- x                 // Assign separately if needed
  // Or use explicit arithmetic:
  y <- x + 1             // Clear intent, predictable

STATEMENT VS EXPRESSION

Statement context (allowed):

  counter++              // Standalone increment
  counter--              // Standalone decrement

Expression context (not allowed):

  result <- counter++    // ERROR: no value returned
  list += counter--      // ERROR: no value returned

See Q50 for loop alternatives. See Q283 for loop patterns without break.

Example

defines module qa.sanitizeddeep.incrementstatement

  defines program

    IncrementStatementDemo()
      stdout <- Stdout()

      // === CORRECT: Statement-only increment ===

      counter <- 0

      counter++
      stdout.println(`After increment: ${counter}`)

      counter++
      stdout.println(`After second increment: ${counter}`)

      counter--
      stdout.println(`After decrement: ${counter}`)

      // === CORRECT: Explicit arithmetic for expressions ===

      baseValue <- 10
      nextValue <- baseValue + 1
      stdout.println(`Next value: ${nextValue}`)

      previousValue <- baseValue - 1
      stdout.println(`Previous value: ${previousValue}`)

      // === CORRECT: Loop with separate increment ===

      loopCount <- 0
      iterations <- 5
      while loopCount < iterations
        stdout.println(`Iteration ${loopCount}`)
        loopCount++

Common mistakes

E50050 — The ++ and -- operators are statement-only in EK9. They cannot be used in expression context where a value is expected. Use explicit arithmetic like 'nextValue <- counter + 1' instead. See ek9 -h E50050 for details.

Incorrect:

nextValue <- counter++

Correct:

counter++
Other ways to ask this
  • What is E07950 increment in expression context?
  • Why can't I use x++ in an assignment?
  • How do I increment and assign in EK9?

Coming from another language?

Java: x++ returns old value, ++x returns new value (confusing). C++: same semantics, major bug source. Python: no ++ operator. Go: ++ is statement-only (same as EK9). Rust: no ++ operator. EK9: ++ and -- are statement-only, use explicit arithmetic for expressions.

Keywords: expression, migrate, operator, E07950, sanitize, decrement, security, increment, mutation, statement, validate