Chapter 13: data, an object and a function, built

Chapter 13 sets out three ways to build: a record for data, a class for an object that looks after its data, and a function for behaviour with no state. This lesson builds one of each, side by side in one file, and a program that uses all three. The IDE's tools make each shell and signature; what is left to type is only the logic no tool can write. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-two-ways.

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

  defines record
    Point
      x as Float: 0.0
      y as Float: 0.0

      operator $ as pure
        <- rtn as String: `(${x}, ${y})`

      default operator ?

  defines class
    Counter
      count as Integer: 0

      increment()
        count += 1

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

      default operator ?

  defines function
    add() as pure
      ->
        a as Integer
        b as Integer
      <- rtn as Integer: a + b

  defines program
    Show()
      stdout <- Stdout()

      p <- Point()
      p.x := 3.0
      p.y := 4.0
      stdout.println(`Point ${p}`)

      tally <- Counter()
      tally.increment()
      tally.increment()
      stdout.println(`Count ${tally.current()}`)

      stdout.println(`Add ${add(2, 3)}`)

Step 1: Data: a record

Choose Code > New Construct > Record…

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

Point is the record's name. Add x and y, both Float, each starting at 0.0 - a record is just named data. Choose Preview.

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

The preview shows Point with both fields - and default operator ? with them: a record that holds fields must say when it counts as set. Choose Apply.

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

Right-click the Point line and choose Add Operator > $ (string) > skeleton - a text form for a point.

The EK9 IDE once this is done: Right-click the Point line and choose Add Operator > $ (string) > skeleton - a text form for a point

Select the stub line and type $'s own text form - just the two numbers in parentheses. Saves when typed.

The EK9 IDE once this is done: Select the stub line and type $'s own text form - just the two numbers in parentheses

Step 2: An object: 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. Type Counter and choose Preview.

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

The preview shows Counter about to be written, in a defines class block of its own beside the record. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Counter about to be written, in a defines class block of its own beside the record

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 - the data the object looks after. Choose OK.

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

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

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

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…

increment is the method's name - no return, not pure: it changes count. Type increment and choose Apply.

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

Select the stub, from ready <- true down to its //AI line, and type count += 1. 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

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…

current is the name, Integer the return type, and pure ticked - it only reads count. Fill them in and choose Apply.

The EK9 IDE just before choosing Apply: current is the name, Integer the return type, and pure ticked - it only reads count

Select the stub, from <- rtn as Integer: 0 down to its //AI line, and type <- rtn <- Integer(count) - a copy of the count, not the field itself. Saves when typed.

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

Step 3: Behaviour: a function

Choose Code > New Construct > Function…

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

add is the function's name. Add two parameters, a and b, both Integer, set Returns to Integer and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: add is the function's name

The preview shows add about to be written, pure, a and b in and an Integer out. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows add about to be written, pure, a and b in and an Integer out

Select the stub, from <- rtn as Integer? down to its //AI line, and type the sum, returned as rtn: a + b. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Integer? down to its //AI line, and type the sum, returned as rtn

Step 4: One program, all three

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: a Point filled in, a Counter ticked twice, and add called once. 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 5: 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.

Point (3.0, 4.0), Count 2, Add 5 - the record, the class and the function, each doing its own job. Choose Close.

The EK9 IDE just before choosing Close: Point (3.0, 4.0), Count 2, Add 5 - the record, the class and the function, each doing its own job

The result

ThreeWays.ek9 now reads exactly as chapter 13 of Getting Started with EK9 shows it: data, an object and a behaviour of its own, side by side in one file and used by one program - built by the tools, logic you typed. ek9_new_construct made the record, class, function and program shells; ek9_add_operator, ek9_add_field and ek9_add_method filled them in, the same doors an AI assistant calls directly.

The EK9 IDE when the lesson has finished

Where this lesson comes from