Chapter 19: a component, built

Chapter 19 builds a component: Pricing, an interface hiding the types that work out a price - a TaxTable class and a basePrice function - behind ONE method, totalPrice. The program depends on Pricing alone; it never mentions TaxTable or basePrice. The IDE's own tools make the structure - each class, function and component's whole signature - so what is left to type is only the logic no tool can write: what surcharge and basePrice return, how StandardPricing works out its answer, what Show prints. To play it in the IDE: Help > Lessons > Writing code…, or ek9 -ide -lesson book-components.

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

  defines class
    TaxTable
      surcharge()
        <- rtn as Money: 2#GBP

  defines function
    basePrice()
      <- rtn as Money: 10#GBP

  defines component
    Pricing as abstract
      totalPrice() as abstract
        <- rtn as Money?

    StandardPricing is Pricing
      override totalPrice()
        <- rtn as Money?
        table <- TaxTable()
        rtn: basePrice() + table.surcharge()

  defines program
    Show()
      stdout <- Stdout()
      pricing as Pricing: StandardPricing()
      stdout.println(`total price: ${pricing.totalPrice()}`)

Step 1: TaxTable — and surcharge

Choose Code > New Construct > Class…

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

TaxTable is the class's name - a helper behind the interface, never mentioned by the program. Type TaxTable and choose Preview.

The EK9 IDE just before choosing Preview: TaxTable is the class's name - a helper behind the interface, never mentioned by the program

The preview shows TaxTable about to be written - a shell, nothing inside it yet. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows TaxTable about to be written - a shell, nothing inside it yet

Right-click the TaxTable line and choose Add Method…

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

surcharge is the method's name, Money the return type - not pure: a real TaxTable would look this up. Fill them in and choose Apply.

The EK9 IDE just before choosing Apply: surcharge is the method's name, Money the return type - not pure

Select the stub, from <- rtn as Money: Money() down to its //AI line, and type surcharge's whole job: a flat 2 GBP. Saves when typed.

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

Step 2: basePrice

Choose Code > New Construct > Function…

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

basePrice is the function's name - no parameters, Money the return type. Fill them in and choose Preview.

The EK9 IDE just before choosing Preview: basePrice is the function's name - no parameters, Money the return type

The preview shows basePrice about to be written, Money out, no parameters in. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows basePrice about to be written, Money out, no parameters in

Select the stub, from <- rtn as Money: Money() down to its //AI line, and type basePrice's whole job: a flat 10 GBP. Saves when typed.

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

Step 3: Pricing — the interface

Choose Code > New Construct > Component…

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

Pricing is the component's name - the interface everything else hides behind. Choose Abstract under Extensibility, then Preview.

The EK9 IDE just before choosing Preview: Pricing is the component's name - the interface everything else hides behind

The preview shows Pricing about to be written, as abstract - a component can be declared abstract exactly like a class. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Pricing about to be written, as abstract - a component can be declared abstract exactly like a class

Right-click the Pricing line and choose Add Method…

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

totalPrice is the method's name, Money the return type, abstract ticked - Pricing only NAMES the method; StandardPricing will answer it. Fill them in and choose Apply.

The EK9 IDE just before choosing Apply: totalPrice is the method's name, Money the return type, abstract ticked - Pricing only NAMES the method; StandardPricing will answer it

Step 4: StandardPricing — and its logic

Choose Code > New Construct > Component…

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

StandardPricing is the component's name - left Closed, nothing else. Type Pricing into Extends, naming the interface StandardPricing completes. Type StandardPricing, fill in Extends, and choose Preview.

The EK9 IDE just before choosing Preview: StandardPricing is the component's name - left Closed, nothing else

The preview shows StandardPricing is Pricing about to be written - and, because Pricing owes one abstract method, its override stub comes with it: totalPrice(), a placeholder until typed. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows StandardPricing is Pricing about to be written - and, because Pricing owes one abstract method, its override stub comes with it

totalPrice()'s override stub arrived with StandardPricing itself - select it, the <- rtn as Money: Money() line down to its //AI line, and type the real answer: a TaxTable and basePrice added together. Saves when typed.

The EK9 IDE once this is done: totalPrice()'s override stub arrived with StandardPricing itself - select it, the <- rtn as Money

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 to bind, no parameters, nothing injected. Type Show and choose Preview.

The EK9 IDE just before choosing Preview: Show is the program's name - no application to bind, no parameters, nothing injected

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, pricing held as the Pricing INTERFACE (never StandardPricing), and a line printing its 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.

total price: 12.00#GBP - the base price plus the surcharge, worked out behind the Pricing interface the program never looks past. Choose Close.

The EK9 IDE just before choosing Close: total price

The result

Pricing.ek9 now reads exactly as chapter 19 of Getting Started with EK9 shows it: a component hiding a class and a function behind an interface, logic you typed - StandardPricing's override arriving WITH the component itself, the moment its Extends names Pricing. The same tools - ek9_new_construct (class, function, component x2), ek9_add_method (x2), 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