Fix Purity Contract

Fix Purity Contract adds, or removes, 'as pure' on the callable a purity error names - with the override cascade applied automatically. EK9 purity is about mutation and non-pure calls, not input or output: stdout.println is itself declared pure, so printing is always allowed inside a pure method; what pure forbids is a mutation operator (+=, :=:, ...) and calling something that is not pure. Here Measure is a pure abstract function, so WordLength, which overrides it, must be pure too - EK9 refuses it otherwise (E05150), explained in the Hovers panel; the opposite mismatch is E05160, and a pure function calling a non-pure one is E08130 - the same tool fixes all three. To play it in the IDE: Help > Lessons > Refactoring…, or ek9 -ide -lesson purity-contract.

The EK9 IDE as the lesson opens: the code to read and, in the Hovers panel, its error explained

Start from

The code the lesson starts from: copy it into a file of your own and follow the steps.

Fails with E05150: 'pure' in super requires 'pure' for this definition

#!ek9
defines module error.e05150.bad

  defines function

    Measure() as pure abstract
      -> text as String
      <- size as Integer?

    WordLength() is Measure
      -> text as String
      <- size as Integer: length text

Step 1: Where the error is

Put the caret on WordLength, on line 10 - the override the compiler flagged.

The EK9 IDE once this is done: Put the caret on WordLength, on line 10 - the override the compiler flagged

Step 2: Blast Radius: the fix it offers

Choose Analyse > Blast Radius. It draws Measure, the contract, in gold, and WordLength under it as fixable, and states the one fix there is - add pure to WordLength - with an Apply button beside it. Press that Apply.

The EK9 IDE just before choosing 🔧 Apply — Add pure to WordLength(): Choose Analyse > Blast Radius

Step 3: Preview and apply

The preview shows WordLength gaining 'as pure'; then the IDE formats the file. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows WordLength gaining 'as pure'; then the IDE formats the file

The result

That is Fix Purity Contract: WordLength is now 'as pure', matching what Measure promises. The same picture - the Blast Radius pane draws - is what ek9_query_purity_impact answers read-only; AI assistants call ek9_fix_purity_contract directly, with the diagnostic's file, line and mode.

The EK9 IDE when the lesson has finished

Where this lesson comes from