Show me how to use ++ on an enumeration to walk through ProcessState values (Pending → Running → Complete), demonstrating that walking past the last value makes the variable unset.
← Operators and Expressions · Ref: Q1262
EK9 enumerations support ++ (next value) and -- (previous value). Each ++ advances to the next declared value; -- moves to the previous one. When the variable would go past the FIRST or LAST declared value, it becomes UNSET — no exception, no wrap-around.
DEFINE THE ENUMERATION
ProcessState
Pending
Running
Complete
Four declared values in order. Pending is first; Complete is last.
WALKING FORWARD
state as ProcessState: ProcessState.Pending stdout.println(`start: ${state}`)
state++ // Running stdout.println(`after ++ : ${state}`)
state++ // Complete stdout.println(`after ++ : ${state}`)
state++ // unset — past the last value stdout.println(`past last is set: ${state?}`)
The critical safety property: incrementing past the last value does NOT throw and does NOT wrap to the first. The variable becomes UNSET, which the ? operator detects.
WALKING BACKWARDS
state2 as ProcessState: ProcessState.Complete state2-- // Running state2-- // Pending state2-- // unset — before the first value stdout.println(`before first is set: ${state2?}`)
SAFE CYCLING PATTERN
current as ProcessState: ProcessState.Pending while current? stdout.println(`processing: ${current}`) current++
The loop terminates automatically when current becomes unset after the last value. No manual bounds tracking, no off-by-one errors.
WHY THIS MATTERS
Most languages either throw on out-of-bounds, wrap to the first value, or require manual bounds checking. EK9's unset-on-boundary semantics compose naturally with the ? operator and guard expressions.
See Q1259 for Integer ++/--. See Q1260 for Float ++/--. See Q1261 for Date ++/--. See Q220 for full enumeration navigation.
Example
defines module qa.operators.incrementenum defines type ProcessState Pending Running Complete defines program IncrementEnumDemo() stdout <- Stdout() state as ProcessState: ProcessState.Pending stdout.println(`start: ${state}`) state++ stdout.println(`after ++ : ${state}`) state++ stdout.println(`after ++ : ${state}`) state++ stdout.println(`past last is set: ${state?}`) state2 as ProcessState: ProcessState.Complete state2-- state2-- state2-- stdout.println(`before first is set: ${state2?}`)
Common mistakes
E50060 — EK9 enumerations do not have a next() method. The compiler cannot resolve the method call. Use the ++ operator to advance to the next declared value. See Q220 for the full enumeration navigation API. See ek9 -h E50060 for details.
Incorrect:
state.next()
Correct:
state++
Other ways to ask this
- How does ++ work on an EK9 enumeration?
- Walk through enumeration values using ++ in EK9.
- Show me what happens when I increment past the last enum value.
- Use the ++ and -- operators on a custom enum like ProcessState.
Coming from another language?
Java: enum.ordinal() + 1 with manual bounds and ArrayIndexOutOfBoundsException risk. Kotlin: similar to Java. Rust: requires implementing Add or using strum crate. Python: no built-in next/previous on Enum. Go: iota integers with silent wrap-around. C#: cast to int and increment, no bounds checking. EK9: ++/-- with automatic boundary-to-unset semantics — the safest design in this list.
Keywords: enumeration, enum, next value, unset boundary, ProcessState, --, ++, previous value