E07950: '++' and '--' are statement-only operators in EK9 - they cannot be used in expressions
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
- Use '++' or '--' as standalone statement
- Or use explicit arithmetic (x + 1) for expression context
See also
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:
- Q664: Why are ++ and -- statement-only operators in EK9?
- Q780: Why can't I use ++ or -- in an expression in EK9?
- Q905: What happens when I use mutating operators in EK9?
- Q940: Can I use ++ in an assignment like y <- x++ in EK9?
- Q967: Does EK9 have ++ and -- operators? What are the rules?
- Q996: Show me ++ and -- being used in a real EK9 program.
- Q1057: Does ++ return a value in EK9? Can I write x <- y++?
- Q1103: Increment an Integer, a Float, and a Character value.
- Q1259: Show me how to use the ++ and -- operators on an Integer counter, demonstrating that they are statement-only and modify the variable in place.
- Q1260: Show me how to use ++ and -- on a Float temperature reading, demonstrating that floating-point types also support in-place increment and decrement.
Found a problem?
If EK9 reported E07950 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.