E07950: '++' and '--' are statement-only operators in EK9 - they cannot be used in expressions

Category: E07xxx Full resolution · Reported in phase: FULL_RESOLUTION · Classification: INCREMENT_DECREMENT_EXPRESSION_NOT_ALLOWED · All error codes

What went wrong

The '++' and '--' operators are STATEMENT-ONLY in EK9. They CANNOT be used in expressions where a value is expected.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 780 (Why can't I use ++ or -- in an expression in EK9?)

Rationale

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

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

The Java case is particularly dangerous - subsequent changes to x unexpectedly affect y. EK9 eliminates this class of bugs.

Key principles

  • '++' and '--' MUTATE the variable in-place
  • They return VOID (no value to assign)
  • Use them only as standalone statements

How to fix it

  1. Use '++' or '--' as standalone statement
  2. Or use explicit arithmetic (x + 1) for expression context

See also

  • E07950: increment/decrement in expression (this error)
  • E07510: mutating operator with pure modifier

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07950: '++' and '--' are statement-only operators in EK9 - they cannot be used in expressions

#!ek9
defines module error.e07950.bad

  defines function

    countUp()
      <- rtn as Integer: 5

      //A statement on its own
      rtn++

    nextOf()
      -> start as Integer
      //The number after start
      <- rtn as Integer: start++

Fixed: Use '++' and '--' as statements

#!ek9
defines module error.e07950.good

  defines function

    countUp()
      <- rtn as Integer: 5

      //A statement on its own
      rtn++

    nextOf()
      -> start as Integer
      //The number after start
      <- rtn as Integer: start + 1

Questions and answers

These Q&A entries show E07950 and its fix: