Chapter 41: substitution, as the compiler checks it, built

Liskov's rule says a subtype must be usable wherever its parent is. Java leaves that to convention; here the compiler checks it. Shape declares area() as pure and abstract - a promise that asking for an area changes nothing - and two subtypes keep it. The IDE's tools write the classes, the field, the operator and the constructor; you type each formula, the function that knows no subtype, and the program. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-substitution.

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.payoffs.shapes

  defines class

    Shape as abstract
      area() as pure abstract
        <- rtn as Float?

    Square extends Shape
      side <- Float()

      Square()
        -> side as Float
        this.side: side

      override area() as pure
        <- rtn as Float: side * side

      override operator ? as pure
        <- rtn as Boolean: side?

    Rectangle extends Shape
      width <- Float()
      height <- Float()

      Rectangle()
        ->
          width as Float
          height as Float

        this.width: width
        this.height: height

      override area() as pure
        <- rtn as Float: width * height

      override operator ? as pure
        <- rtn as Boolean: width? and height?

  defines function

    describe() as pure
      -> shape as Shape
      <- rtn as String: `area ${shape.area()}`

  defines program

    Substitution()
      stdout <- Stdout()
      shapes <- List() of Shape
      shapes += Square(3.0)
      shapes += Rectangle(2.0, 5.0)
      for shape in shapes
        stdout.println(describe(shape))

//EOF

Step 1: Shape — pure area(), declared once

Choose Code > New Construct > Class…

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

Shape is the class's name. Shape is a contract. Choose Abstract under Extensibility. Choose Preview.

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

The preview shows Shape as abstract about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Shape as abstract about to be written

Select the Shape 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 Shape line and, above it, type the blank line the book opens its first class with

Right-click the Shape line and choose Add Method…

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

area is the method's name. Pure is the promise every subtype must keep; abstract means there is no body here, only the contract. Set Return type to Float, tick pure and abstract. Choose Apply.

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

Add Method applies straight away but leaves the editor unsaved; save before starting Square. Choose File > Save.

The EK9 IDE once this is done: Add Method applies straight away but leaves the editor unsaved; save before starting Square

Step 2: Square — a subtype

Choose Code > New Construct > Class…

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

Square is the class's name. Type Shape into Extends. Choose Preview.

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

The preview shows Square extends Shape about to be written - and, because Shape owes area(), its override stub comes with it: pure, as the parent promised. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Square extends Shape about to be written - and, because Shape owes area(), its override stub comes with it

Right-click the Square line and choose Add Field…

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

Type side <- Float() - the one measurement - a Float that starts unset. Choose OK.

The EK9 IDE just before choosing OK: Type side <- Float() - the one measurement - a Float that starts unset

The preview shows side added - and, with the first field, default operator ?. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows side added - and, with the first field, default operator ?

Select the default operator ? line and delete it: the chapter writes ? by hand, so the menu will not offer it while a default one stands. Saves when removed.

The EK9 IDE once this is done: Select the default operator ? line and delete it

Right-click the Square line and choose Add Operator > ? (isSet) > skeleton: the chapter writes ? by hand as an override, because a subtype that adds fields must say what 'set' means for them.

The EK9 IDE once this is done: Right-click the Square line and choose Add Operator > ? (isSet) > skeleton

Right-click the Square line and choose Add Constructor > From All Fields: one parameter per field, each assigned with :.

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

Select area()'s stub, from <- rtn as Float: 0.0 down to its //AI line, and type Square's formula: side * side. It stays pure. Saves when typed.

The EK9 IDE once this is done: Select area()'s stub, from <- rtn as Float

Step 3: Rectangle — another subtype

Choose Code > New Construct > Class…

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

Rectangle is the class's name. Type Shape into Extends. Choose Preview.

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

The preview shows Rectangle extends Shape about to be written - and, because Shape owes area(), its override stub comes with it: pure, as the parent promised. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Rectangle extends Shape about to be written - and, because Shape owes area(), its override stub comes with it

Right-click the Rectangle line and choose Add Field…

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

Type width <- Float() - the two measurements - each a Float that starts unset. Choose OK.

The EK9 IDE just before choosing OK: Type width <- Float() - the two measurements - each a Float that starts unset

The preview shows width added - and, with the first field, default operator ?. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows width added - and, with the first field, default operator ?

Right-click the Rectangle line and choose Add Field…

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

Type height <- Float() - the two measurements - each a Float that starts unset. Choose OK.

The EK9 IDE just before choosing OK: Type height <- Float() - the two measurements - each a Float that starts unset

The preview shows height added - and, with the first field, default operator ?. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows height added - and, with the first field, default operator ?

Select the default operator ? line and delete it: the chapter writes ? by hand, so the menu will not offer it while a default one stands. Saves when removed.

The EK9 IDE once this is done: Select the default operator ? line and delete it

Right-click the Rectangle line and choose Add Operator > ? (isSet) > skeleton: the chapter writes ? by hand as an override, because a subtype that adds fields must say what 'set' means for them.

The EK9 IDE once this is done: Right-click the Rectangle line and choose Add Operator > ? (isSet) > skeleton

Right-click the Rectangle line and choose Add Constructor > From All Fields: one parameter per field, each assigned with :.

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

Select area()'s stub, from <- rtn as Float: 0.0 down to its //AI line, and type Rectangle's formula: width * height. It stays pure. Saves when typed.

The EK9 IDE once this is done: Select area()'s stub, from <- rtn as Float

Step 4: describe — a caller that knows no subtype

Choose Code > New Construct > Function…

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

describe is the function's name. Add shape of type Shape, set Returns to String and tick pure. Its parameter is the PARENT type: this is the function that must work for every subtype. Choose Preview.

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

The preview shows describe about to be written, pure, a Shape in and a String out. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows describe about to be written, pure, a Shape in and a String out

Select the line above describe and type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the line above describe and type the blank line the book opens its first function with

Select the stub, from <- rtn as String? down to its //AI line, and type describe's body: it asks the shape for its area() and never says which shape it is. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String? down to its //AI line, and type describe's body

Step 5: The program

Choose Code > New Construct > Program…

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

Substitution is the program's name. Choose Preview.

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

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

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

Select the Substitution 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 Substitution 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 Substitution's body: a List of Shape holding a Square and a Rectangle, and a loop that describes each - one call, whichever subtype it is. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type Substitution'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.

area 9.0 and area 10.0 - one loop, one call to describe each time, and the right formula runs for each shape without describe ever asking which one it is. Choose Close.

The EK9 IDE just before choosing Close: area 9.0 and area 10.0 - one loop, one call to describe each time, and the right formula runs for each shape without describe ever asking which one it is

The result

Substitution.ek9 now reads exactly as chapter 41 of EK9 in Depth shows it. describe knows nothing of Square or Rectangle, and area() is declared pure on Shape, so a caller can rely on it being a query - and the compiler holds each subtype to that: an override that drops pure is E05150, one that forgets override is E05120, and a subtype that adds fields must declare its own operators (E07238). The tools - ek9_new_construct, ek9_add_method, ek9_add_field, ek9_add_operator and ek9_add_constructor - are what an AI assistant calls to do the same thing.

The EK9 IDE when the lesson has finished

Where this lesson comes from