Chapter 16: a class, built

Chapter 16 builds a class: Counter, counting ticks, and the program that runs it three times. The IDE's own tools make the structure - the class, the field's declaration, each method's whole signature, the program's shell - so what is left to type is only the logic no tool can write: what tick does, what total returns, what Show prints. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-classes.

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

  defines class
    Counter
      count as Integer: 0

      tick()
        count += 1

      total() as pure
        <- rtn <- Integer(count)

      default operator ?

  defines program
    Show()
      stdout <- Stdout()
      clicks <- Counter()
      clicks.tick()
      clicks.tick()
      clicks.tick()
      stdout.println(`clicks so far: ${clicks.total()}`)

Step 1: A class

Choose Code > New Construct > Class…

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

Counter is the class's name. Closed is already selected - EK9's default: a class is closed to extension unless it says 'as open' or 'as abstract'. Choose Preview.

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

The preview shows Counter about to be written - a shell, nothing inside it yet; then the IDE formats the file it writes. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Counter about to be written - a shell, nothing inside it yet; then the IDE formats the file it writes

Step 2: A field

Right-click the Counter line and choose Add Field…

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

Type count as Integer: 0 - a field, its type, and the value it starts with. Choose OK.

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

The preview shows count added - and default operator ? with it: a class that holds fields must say when it counts as set (EK9's tri-state rule), so adding the first field added that operator too. Choose Apply.

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

Step 3: tick — and its logic

Right-click the Counter line and choose Add Method…

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

tick is the method's name. It changes count, so it is not pure. Choose Apply.

The EK9 IDE just before choosing Apply: tick is the method's name

Select the stub, from ready <- true down to its //AI line, and type count += 1 - tick's whole job. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type count += 1 - tick's whole job

Step 4: total — and its logic

Right-click the Counter line and choose Add Method…

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

total is the method's name. total only reads count, it changes nothing. Set Return type to Integer and tick pure. Choose Apply.

The EK9 IDE just before choosing Apply: total is the method's name

Select the stub, from <- rtn as Integer: 0 down to its //AI line, and type <- rtn <- Integer(count) - total hands back count, as an Integer in its own right. Saves when typed.

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

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. No application, no parameters, nothing injected: the plain shape every Getting Started program starts from. 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: a Stdout, a Counter, three ticks, and a line printing the total. 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.

clicks so far: 3 - tick() ran three times, and total() reads count back as an Integer. Choose Close.

The EK9 IDE just before choosing Close: clicks so far

The result

Counter.ek9 now reads exactly as chapter 16 of Getting Started with EK9 shows it: a class the tools built, logic you typed. The same four tools - ek9_new_construct, ek9_add_field, ek9_add_method, 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