Chapter 25: one split, + and +=, built

EK9 keeps the spelling of operators and removes the freedom: the set is closed, every signature is fixed, and every operator either mutates its receiver or does not. Chapter 25's OperatorSplit shows the split on one record, Credit: + computes a new value and must be pure, += changes the receiver and must not be. The IDE writes the record, both constructors and the signature of each operator - + and += through Custom Operator…, $ from its own menu entry; you type what each one does. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-operator-split.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.operatorsplit

  defines record

    Credit
      pence <- Integer()

      default Credit() as pure

      Credit() as pure
        -> amount as Integer
        pence :=? Integer(amount)

      operator + as pure
        -> other as Credit
        <- rtn as Credit: Credit(pence + other.pence)

      operator +=
        -> other as Credit
        pence += other.pence

      operator $ as pure
        <- rtn as String: `${pence}p`

      default operator ?

  defines program

    OperatorSplit()
      stdout <- Stdout()

      deposit <- Credit(150)
      bonus <- Credit(250)

      sum <- deposit + bonus
      stdout.println(`sum ${sum}, deposit still ${deposit}`)

      deposit += bonus
      stdout.println(`after +=, deposit ${deposit}`)

Step 1: Credit — a record

Choose Code > New Construct > Record…

The EK9 IDE once this is done: Choose Code > New Construct > Record…

Credit is the record's name. The Fields table holds pence, starting at Integer(). A Type left blank is taken from the value. Choose Preview.

The EK9 IDE just before choosing Preview: Credit is the record's name

The preview shows Credit about to be written - and default operator ? with it: a record that holds fields must say when it counts as set. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Credit about to be written - and default operator ? with it

Select the Credit line and, above it, type the blank line the book opens its records with. Saves when typed.

The EK9 IDE once this is done: Select the Credit line and, above it, type the blank line the book opens its records with

Right-click the Credit line and choose Add Constructor > No-Arg (default, pure). It is the compiler's own standard version, which leaves every field unset.

The EK9 IDE once this is done: Right-click the Credit line and choose Add Constructor > No-Arg (default, pure)

Right-click the Credit line and choose Add Constructor > From All Fields (pure) - a constructor that takes every field.

The EK9 IDE once this is done: Right-click the Credit line and choose Add Constructor > From All Fields (pure) - a constructor that takes every field

Select the stub, from Credit() as pure down to its last assignment, and type the constructor's whole job as the book writes it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from Credit() as pure down to its last assignment, and type the constructor's whole job as the book writes it

Right-click the Credit line and choose Add Operator > Custom Operator…

The EK9 IDE once this is done: Right-click the Credit line and choose Add Operator > Custom Operator…

The EK9 IDE just before choosing OK: + is not one of the eight operators with their own menu entry - type it here, in Custom Operator…'s one field

Select the stub, from <- rtn as Credit: this down to its //AI lines, and type +'s return: a new Credit holding the sum. + makes a new value, so it is pure. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Credit

Right-click the Credit line and choose Add Operator > Custom Operator…

The EK9 IDE once this is done: Right-click the Credit line and choose Add Operator > Custom Operator…

+= is not one of the eight operators with their own menu entry - type it here, in Custom Operator…'s one field. Choose OK.

The EK9 IDE just before choosing OK: += is not one of the eight operators with their own menu entry - type it here, in Custom Operator…'s one field

Select the //AI lines and the require, and type +='s one line: it adds into this Credit, so it changes its receiver - and a mutating operator must not be pure. Saves when typed.

The EK9 IDE once this is done: Select the //AI lines and the require, and type +='s one line

Right-click the Credit line and choose Add Operator > $ (string) > skeleton.

The EK9 IDE once this is done: Right-click the Credit line and choose Add Operator > $ (string) > skeleton

Select the stub line and type $'s own text form: the pence, then a p. Saves when typed.

The EK9 IDE once this is done: Select the stub line and type $'s own text form

Step 2: The program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

OperatorSplit is the program's name. Choose Preview.

The EK9 IDE just before choosing Preview: OperatorSplit is the program's name

The preview shows OperatorSplit about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows OperatorSplit about to be written

Select the OperatorSplit() line and, above it, type the blank line the book opens the program with. Saves when typed.

The EK9 IDE once this is done: Select the OperatorSplit() line and, above it, type the blank line the book opens the program with

Select the stub, from ready <- true down to its //AI line, and type OperatorSplit's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type OperatorSplit's body exactly as the chapter shows it

Step 3: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

sum 400p, deposit still 150p - then, after +=, deposit 400p. Choose Close.

The EK9 IDE just before choosing Close: sum 400p, deposit still 150p - then, after +=, deposit 400p

The result

OperatorSplit.ek9 now reads exactly as chapter 25 of EK9 in Depth shows it. The pure + left deposit alone; the += without pure changed it. Mark either one the other way round and the compiler refuses it (E07500, E07510).

The EK9 IDE when the lesson has finished

Where this lesson comes from