E07500: operator must be declared pure

Category: E07xxx Full resolution · Reported in phase: EXPLICIT_TYPE_SYMBOL_DEFINITION · Classification: OPERATOR_MUST_BE_PURE · All error codes

What went wrong

Certain operators MUST be marked 'pure' because they should never have side effects. You've defined one without 'pure'.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 239 (How do comparison and ordering operators work in EK9?)
  • ek9 -q 238 (What operators does EK9 support and why is the set fixed?)
  • ek9 -q 96 (How do operators work in EK9?)
  • ek9 -q 242 (How do conversion and introspection operators work in EK9?)
  • ek9 -q 98 (How do record operators work in EK9?)

Operators that must be pure

==, <>, <, <=, >, >=, <=>          // Comparison
?                                   // Set check
$, $$                               // String conversion
#?                                  // Hashcode

Rationale

Comparison/query operations shouldn't modify state.

How to fix it

  1. Add 'as pure' modifier

See also

  • E07500: pure operator missing 'pure' (this error)
  • E07510: mutating operator has 'pure'

Examples

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

Fails with E07500: operator must be declared pure

#!ek9
defines module error.e07500.bad

  defines class

    Basket
      items <- ["apple", "pear"]

      //A query operator must not change anything
      default operator ?

      operator contains
        -> item as String
        <- rtn as Boolean: items contains item

Fixed: Mark it 'as pure'

#!ek9
defines module error.e07500.good

  defines class

    Basket
      items <- ["apple", "pear"]

      default operator ?

      operator contains as pure
        -> item as String
        <- rtn as Boolean: items contains item

Questions and answers

These Q&A entries show E07500 and its fix: