Chapter 16: a class that hands out a copy, built

Chapter 16's ScoreBoard keeps a list of scores and the best so far, and answers summary() with a Summary record that holds copies - so a snapshot never changes behind your back. The IDE's tools make the record, the class, its fields and both methods' signatures; you type only the logic. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-scoreboard.

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

  defines record
    Summary
      count as Integer: Integer()
      best as Integer: Integer()

      Summary() as pure
        ->
          c as Integer
          b as Integer

        count :=? Integer(c)
        best :=? Integer(b)

      operator $ as pure
        <- rtn as String: `${count} scores, best ${best}`

      default operator ?

  defines class
    ScoreBoard
      scores as List of Integer: List() of Integer
      topScore as Integer: Integer()

      add()
        -> points as Integer
        scores += Integer(points)
        if not topScore? or points > topScore
          topScore := Integer(points)

      summary() as pure
        <- rtn <- Summary(length scores, topScore)

      default operator ?

  defines program
    Show()
      stdout <- Stdout()
      board <- ScoreBoard()
      board.add(30)
      board.add(70)

      snap <- board.summary()
      stdout.println(`snapshot after two: ${snap}`)

      board.add(90)
      stdout.println(`snapshot is unchanged: ${snap}`)
      stdout.println(`live summary now:     ${board.summary()}`)

Step 1: A record for the figures

Choose Code > New Construct > Record…

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

Summary is the record's name. Add count and best, both Integer, each starting at Integer(). Choose Preview.

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

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

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

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

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

Select the stub, from Summary() as pure down to its last assignment, and type the constructor the book shows: c and b in, count and best set with :=?. Saves when typed.

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

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

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

Select the stub line and type $'s own text form - the count and the best, in words. Saves when typed.

The EK9 IDE once this is done: Select the stub line and type $'s own text form - the count and the best, in words

Step 2: A class

Choose Code > New Construct > Class…

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

ScoreBoard is the class's name. Type ScoreBoard and choose Preview.

The EK9 IDE just before choosing Preview: ScoreBoard is the class's name

The preview shows ScoreBoard about to be written, in a defines class block after the record. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ScoreBoard about to be written, in a defines class block after the record

Step 3: Two fields

Right-click the ScoreBoard line and choose Add Field…

The EK9 IDE once this is done: Right-click the ScoreBoard line and choose Add Field…

Type scores as List of Integer: List() of Integer - a List of Integer, empty to start with. Choose OK.

The EK9 IDE just before choosing OK: Type scores as List of Integer

The preview shows scores added - and default operator ? with it. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows scores added - and default operator ? with it

Right-click the ScoreBoard line and choose Add Field…

The EK9 IDE once this is done: Right-click the ScoreBoard line and choose Add Field…

Type topScore as Integer: Integer() - the best score so far, unset until a score arrives. Choose OK.

The EK9 IDE just before choosing OK: Type topScore as Integer

The preview shows topScore joining scores. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows topScore joining scores

Step 4: add — a method with a parameter

Right-click the ScoreBoard line and choose Add Method…

The EK9 IDE once this is done: Right-click the ScoreBoard line and choose Add Method…

add is the method's name - no return, not pure: it changes the board. Add one parameter, points of type Integer, and choose Apply.

The EK9 IDE just before choosing Apply: add is the method's name - no return, not pure

Select the stub, from require points? down to its //AI line, and type what add does: keep the score and, if it beats the best, make it the best. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require points? down to its //AI line, and type what add does

Step 5: summary — a copy, not a view

Right-click the ScoreBoard line and choose Add Method…

The EK9 IDE once this is done: Right-click the ScoreBoard line and choose Add Method…

summary is the name, Summary the return type, pure ticked - it only reads. Fill them in and choose Apply.

The EK9 IDE just before choosing Apply: summary is the name, Summary the return type, pure ticked - it only reads

Select the stub, from <- rtn as Summary: Summary() down to its //AI line, and type <- rtn <- Summary(length scores, topScore) - a new record built from the current figures. Saves when typed.

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

Step 6: 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: two scores, a snapshot, a third score, and the snapshot printed again beside the live summary. 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 7: 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.

snapshot after two: 2 scores, best 70 - then, after a third score, the snapshot is unchanged while the live summary reads 3 scores, best 90. Choose Close.

The EK9 IDE just before choosing Close: snapshot after two

The result

ScoreBoard.ek9 now reads exactly as chapter 16 of Getting Started with EK9 shows it: a class that hands out a Summary - a record built from copies of its figures - so a snapshot taken early is unchanged by later scores. The tools wrote the structure; the logic is what you typed. ek9_new_construct, ek9_add_constructor, ek9_add_operator, ek9_add_field, ek9_add_method and ek9_add_program are what an AI assistant calls to do the same thing.

The EK9 IDE when the lesson has finished

Where this lesson comes from