Chapter 30: a generic constrained by a trait, built

An unconstrained T is an Any: you can hold it and pass it, and little else. Ledger of type T constrain by Priced says every T must be a Priced, so the body may call price() on a T and compare two of them with >. The chapter's trait declares the ordering, and the compiler holds it to a standard - a Money orders only within one currency, so the version that compares the currency first is the one that is accepted. The IDE writes the trait's method, the two unrelated classes (Ticket and Meal, each with the trait adopted and a field), the generic Ledger and the program's shell; you type the ordering, the constructors and what each method does. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-priced-ledger.

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.generics.ledger

  defines trait

    Priced
      price() as pure abstract
        <- rtn as Money?

      operator <=> as pure
        -> other as Priced
        <- rtn as Integer: #> price() <=> #> other.price()
        if rtn == 0
          rtn: #< price() <=> #< other.price()

      operator #? as pure
        <- rtn as Integer: #?price()

  defines class

    Ticket with trait of Priced
      fare <- Money()

      default Ticket() as pure

      Ticket() as pure
        -> amount as Money
        fare :=? amount

      override price() as pure
        <- rtn as Money: fare

      default operator ?

    Meal with trait of Priced
      cost <- Money()

      default Meal() as pure

      Meal() as pure
        -> amount as Money
        cost :=? amount

      override price() as pure
        <- rtn as Money: cost

      default operator ?

    Ledger of type T constrain by Priced
      items as List of T: List()

      default Ledger()

      Ledger()
        -> first as T
        items += first

      add()
        -> item as T
        items += item

      total()
        <- rtn as Money: 0#GBP
        for item in items
          rtn += item.price()

      dearest()
        <- rtn as Optional of T: Optional()
        for item in items
          keep as Boolean: true
          if rtn?
            current as T: rtn.get()
            keep := item > current
          if keep
            rtn: Optional(item)

      default operator ?

  defines program

    PricedLedgers()
      stdout <- Stdout()

      tickets <- Ledger(Ticket(45#GBP))
      tickets.add(Ticket(12#GBP))
      stdout.println(`tickets total: ${tickets.total()}`)

      meals <- Ledger() of Meal
      meals.add(Meal(9#GBP))
      meals.add(Meal(31#GBP))
      meals.add(Meal(18#GBP))
      stdout.println(`meals total: ${meals.total()}`)
      if dearest <- meals.dearest()
        stdout.println(`dearest meal: ${dearest.get().price()}`)

Step 1: Priced — a trait whose ordering is total

Choose Code > New Construct > Trait…

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

Priced is the trait's name. Choose Preview.

The EK9 IDE just before choosing Preview: Priced is the trait's name

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

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

Select the Priced line and, above it, type the blank line the book opens its first trait with. Saves when typed.

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

Right-click the Priced line and choose Add Method…

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

price is the method's name. Set Return type to Money, tick pure and abstract - a contract with no body. Choose Apply.

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

Below price, type the trait's ordering - a trait with no fields has no skeleton to start from. It compares the currency first (#>) and the amount second (#<), so it answers for every pair of prices; and the hash the ordering needs. Saves when typed.

The EK9 IDE once this is done: Below price, type the trait's ordering - a trait with no fields has no skeleton to start from

Step 2: Ticket — a class that is Priced

Choose Code > New Construct > Class…

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

Ticket is the class's name. Choose Preview.

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

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

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

Select the Ticket line and, above it, type the blank line the book opens its first class with. Saves when typed.

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

Right-click the Ticket line and choose Adopt Trait…

The EK9 IDE once this is done: Right-click the Ticket line and choose Adopt Trait…

Type Priced as the trait - Implement method stubs is already chosen - and choose OK.

The EK9 IDE just before choosing OK: Type Priced as the trait - Implement method stubs is already chosen - and choose OK

Adopt Trait changes the open file in the editor, not yet saved. Choose File > Save.

The EK9 IDE once this is done: Adopt Trait changes the open file in the editor, not yet saved

Right-click the Ticket line and choose Add Field…

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

Type fare <- Money() - the field and the value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type fare <- Money() - the field and the value it starts with

The preview shows fare added - and default operator ? with it, Ticket's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows fare added - and default operator ? with it, Ticket's first field

Right-click the Ticket line and choose Add Constructor > No-Arg (default, pure).

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

Right-click the Ticket 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 Ticket line and choose Add Constructor > From All Fields (pure) - a constructor that takes every field

Select the constructor and type the book's: amount in, fare set with :=?. Saves when typed.

The EK9 IDE once this is done: Select the constructor and type the book's

Select the override's stub and type price's answer: the fare. Saves when typed.

The EK9 IDE once this is done: Select the override's stub and type price's answer

Step 3: Meal — another class that is Priced

Choose Code > New Construct > Class…

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

Meal is the class's name. Choose Preview.

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

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

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

Right-click the Meal line and choose Adopt Trait…

The EK9 IDE once this is done: Right-click the Meal line and choose Adopt Trait…

Type Priced as the trait - Implement method stubs is already chosen - and choose OK.

The EK9 IDE just before choosing OK: Type Priced as the trait - Implement method stubs is already chosen - and choose OK

Adopt Trait changes the open file in the editor, not yet saved. Choose File > Save.

The EK9 IDE once this is done: Adopt Trait changes the open file in the editor, not yet saved

Right-click the Meal line and choose Add Field…

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

Type cost <- Money() - the field and the value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type cost <- Money() - the field and the value it starts with

The preview shows cost added - and default operator ? with it, Meal's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows cost added - and default operator ? with it, Meal's first field

Right-click the Meal line and choose Add Constructor > No-Arg (default, pure).

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

Right-click the Meal 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 Meal line and choose Add Constructor > From All Fields (pure) - a constructor that takes every field

Select the constructor and type the book's: amount in, cost set with :=?. Saves when typed.

The EK9 IDE once this is done: Select the constructor and type the book's

Select the override's stub and type price's answer: the cost. Saves when typed.

The EK9 IDE once this is done: Select the override's stub and type price's answer

Step 4: Ledger — generic, but only over a Priced

Choose Code > New Construct > Class…

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

Ledger is the class's name. Tick Generic and give the type parameter T (constrain by Priced). The Fields table holds items of type List of T, starting at List(). Choose Preview.

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

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

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

Select the stub of the one-item constructor and type the book's: first in, the ledger starting with it. Saves when typed.

The EK9 IDE once this is done: Select the stub of the one-item constructor and type the book's

Right-click the Ledger line and choose Add Method…

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

add is the method's name. Add one parameter, item of type T. Choose Apply.

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

Select the stub and type add's work. Saves when typed.

The EK9 IDE once this is done: Select the stub and type add's work

Right-click the Ledger line and choose Add Method…

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

total is the method's name. Set Return type to Money. Choose Apply.

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

Select the stub and type total's work: price() is callable on every T, because T is Priced. Saves when typed.

The EK9 IDE once this is done: Select the stub and type total's work

Right-click the Ledger line and choose Add Method…

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

dearest is the method's name. Set Return type to Optional of T. Choose Apply.

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

Select the stub and type dearest's work: the > on two Ts is the trait's ordering. Saves when typed.

The EK9 IDE once this is done: Select the stub and type dearest's work

Step 5: The program

Choose Code > New Construct > Program…

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

PricedLedgers is the program's name. Choose Preview.

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

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

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

Select the PricedLedgers() 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 PricedLedgers() 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 PricedLedgers'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 PricedLedgers's body exactly as the chapter shows it

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.

tickets total: 57.00#GBP, meals total: 58.00#GBP, then dearest meal: 31.00#GBP. Choose Close.

The EK9 IDE just before choosing Close: tickets total

The result

PricedLedger.ek9 now reads exactly as chapter 30 of EK9 in Depth shows it. Ticket and Meal are unrelated classes that share one trait, and Ledger works for either - total() calls price() and dearest() compares with >, both of which the constraint put in reach of every T. Ledger of Ticket and Ledger of Meal are two types the compiler made from the one template.

The EK9 IDE when the lesson has finished

Where this lesson comes from