E08240: mutation operators are not applicable to function types, only '?' operator is supported

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

What went wrong

Function types are callable references, not data containers. Mutation operators (:=:, :~:, :^:, +=, -=) are not applicable to function types. Only the '?' (isSet) operator is supported on functions.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 738 (Why does EK9 reject mutation operators on function types?)

How to fix it

  1. Use ':=' (simple assignment) instead of ':=:' (copy) for function variables
  2. Functions are references - reassignment changes which function is referenced
  3. Only '?' (isSet) is valid as an operator on function types

See also

  • E08240: mutation on function type (this error)
  • E07620: operator not defined (unsupported operator on type)

Examples

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

Fails with E08240: mutation operators are not applicable to function types, only '?' operator is supported

#!ek9
defines module error.e08240.bad

  defines class

    TextHandler
      processor as Processor?

      default private TextHandler()

      TextHandler()
        -> handler as Processor
        processor :=: handler

      default operator ?

  defines function

    Processor() as abstract
      -> text as String
      <- rtn as String?

Fixed: Assign the function; only ? applies to it

#!ek9
defines module error.e08240.good

  defines class

    TextHandler
      processor as Processor?

      default private TextHandler()

      TextHandler()
        -> handler as Processor
        processor: handler

      isReady()
        <- rtn as Boolean: processor?

      default operator ?

  defines function

    Processor() as abstract
      -> text as String
      <- rtn as String?

Questions and answers

These Q&A entries show E08240 and its fix: