E08100: reassignment not allowed when scope is marked as 'pure' (':=?' is supported)

Category: E08xxx Pre-IR checks (flow analysis) · Reported in phase: FULL_RESOLUTION · Classification: NO_PURE_REASSIGNMENT · All error codes

What went wrong

In a PURE context, you're using ':=' for reassignment, but this is not allowed. Pure functions cannot reassign variables - except the returning variable, which may be reassigned because it is the result being built.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 790 (Why can't I reassign variables in a pure function?)
  • ek9 -q 563 (How do pure constructors work and why use ':=?' for field...)
  • ek9 -q 565 (How do I write multiple pure constructors with delegation?)
  • ek9 -q 678 (How do pure and non-pure methods coexist in an override h...)

Rationale

Pure functions should compute results, not accumulate state.

How to fix it

  1. Use ':=?' for conditional first-time assignment
  2. Or restructure to avoid reassignment

Common causes

  • Trying to accumulate results in pure function
  • Habit from non-pure code

See also

  • E08100: reassignment in pure context (this error)
  • E08110: parameter reassignment

Examples

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

Fails with E08100: reassignment not allowed when scope is marked as 'pure' (':=?' is supported)

#!ek9
defines module error.e08100.bad

  defines function

    total() as pure
      ->
        price as Integer
        quantity as Integer
      <- rtn as Integer?

      subtotal <- price
      subtotal := price * quantity
      rtn: subtotal

Fixed: Give the variable its value where it is declared

#!ek9
defines module error.e08100.good

  defines function

    total() as pure
      ->
        price as Integer
        quantity as Integer
      <- rtn as Integer?

      subtotal <- price * quantity
      rtn: subtotal

Fixed: Or declare it unset and assign it once with :=?

#!ek9
defines module error.e08100.good.if.unset

  defines function

    total() as pure
      ->
        price as Integer
        quantity as Integer
      <- rtn as Integer?
      subtotal as Integer?
      subtotal :=? price * quantity
      rtn: subtotal

Questions and answers

These Q&A entries show E08100 and its fix: