Chapter 21: every other operator, in one step

Chapter 21's Release is a version number - a major and a minor - that has to print as 1.5 yet sort and compare like any value. One operator is written by hand; the rest come from a single default operator line. The IDE's tools write the record, the constructor's shell and both operators; you type only what no tool can. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-versions.

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.versions

  defines record
    Release
      major as Integer: Integer()
      minor as Integer: Integer()

      Release() as pure
        ->
          initialMajor as Integer
          initialMinor as Integer

        major :=? Integer(initialMajor)
        minor :=? Integer(initialMinor)

      //Written by hand, because "1.5" is our own idea of how a Release reads
      operator $ as pure
        <- rtn as String: `${major}.${minor}`

      //And the compiler fills in every other operator it can work out for itself
      default operator

  defines program
    Show()
      stdout <- Stdout()

      versions <- List() of Release
      versions += Release(2, 0)
      versions += Release(1, 5)
      versions += Release(2, 3)

      sorted <- cat versions | sort | collect as List of Release
      for v in sorted
        stdout.println(`sorted: ${v}`)

      a <- Release(1, 5)
      b <- Release(2, 0)
      stdout.println(`a < b?   ${a < b}`)
      stdout.println(`a > b?   ${a > b}`)
      stdout.println(`as text: ${a}`)
      stdout.println(`as JSON: ${$$a}`)

Step 1: A record

Choose Code > New Construct > Record…

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

Release is the record's name. Add major and minor, both Integer, each starting at Integer(). Choose Preview.

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

The preview shows Release with both fields - and default operator ? with them. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Release with both fields - and default operator ? with them

Step 2: Its constructor

Right-click the Release line and choose Add Constructor > From All Fields (pure).

The EK9 IDE once this is done: Right-click the Release line and choose Add Constructor > From All Fields (pure)

Select the stub, from Release() as pure down to its last assignment, and type the constructor the book shows: initialMajor and initialMinor in, major and minor set with :=?. Saves when typed.

The EK9 IDE once this is done: Select the stub, from Release() as pure down to its last assignment, and type the constructor the book shows

Step 3: One operator by hand

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

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

Select the stub line and type $'s own text form - major, a dot, minor. Saves when typed.

The EK9 IDE once this is done: Select the stub line and type $'s own text form - major, a dot, minor

Select the operator $ line and, above it, type the comment that says why it is written by hand. Saves when typed.

The EK9 IDE once this is done: Select the operator $ line and, above it, type the comment that says why it is written by hand

Step 4: Every other operator, in one step

Right-click the Release line and choose Add Operator > default operator (all missing) - one bare 'default operator' line, and the compiler works out == <=> < <= > >= <> $$ and the rest from the two fields.

The EK9 IDE once this is done: Right-click the Release line and choose Add Operator > default operator (all missing) - one bare 'default operator' line, and the compiler works out == <=> < <= > >= <> $$ and the rest from the two fields

The bare line already includes the ? operator, so delete the default operator ? line above it - the one the record was created with. Saves when typed.

The EK9 IDE once this is done: The bare line already includes the ? operator, so delete the default operator ? line above it - the one the record was created with

Select the default operator line and, above it, type the comment that says what it does. Saves when typed.

The EK9 IDE once this is done: Select the default operator line and, above it, type the comment that says what it does

Step 5: The program

Choose Code > New Construct > Program…

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

Show is the program's name. Type Show and choose Preview.

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

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

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

Select the stub, from ready <- true down to its //AI line, and type Show's body: three releases sorted and printed, then a comparison, the text form and the JSON form. Saves when typed.

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

Step 6: 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.

sorted: 1.5, 2.0, 2.3 - a < b? true, a > b? false, as text 1.5, and as JSON the record's own two fields: every one worked out from a single default operator. Choose Close.

The EK9 IDE just before choosing Close: sorted

The result

Versions.ek9 now reads exactly as chapter 21 of Getting Started with EK9 shows it: a Release that prints as 1.5 because its $ is written by hand, and sorts, compares and turns to JSON because one bare 'default operator' line lets the compiler work the rest out from its two fields. Add Operator ▸ default operator (all missing) wrote that line; expand-default-operator shows how to turn any one of them back into an explicit body. AI assistants use ek9_new_construct, ek9_add_constructor, ek9_add_operator (with no operator named, for the bare line) and ek9_add_program.

The EK9 IDE when the lesson has finished

Where this lesson comes from