Chapter 30: the copy a generic asks every T for, built

Chapter 30 starts with a Shelf that holds Books and is written once over any T, as Shelf of type T. Each filling, Shelf of Book, is a real type the compiler checks - its T is never erased to Object, as Java's is - and the template may only do to a T what every T can do. The IDE writes the Book class, the generic Shelf with its type parameter and its field, the methods' shapes and the program's shell; you type the constructors' logic and what each method does. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-shelf.

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

  defines class

    Book
      name <- String()

      default Book() as pure

      Book() as pure
        -> title as String
        name :=? title

      Book() as pure
        -> other as Book
        name :=? other.name

      title() as pure
        <- rtn as String: name

      default operator ?

    Shelf of type T
      items as List of T: List()

      default Shelf()

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

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

      size() as pure
        <- rtn as Integer: length items

      default operator ?

  defines program

    Library()
      stdout <- Stdout()
      original <- Book("Emma")
      shelf <- Shelf() of Book
      shelf.add(original)
      shelf.add(Book("Persuasion"))
      stdout.println(`on the shelf: ${shelf.size()}`)
      stdout.println(`first title: ${original.title()}`)

Step 1: Book — an ordinary class

Choose Code > New Construct > Class…

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

Book is the class's name. Choose Preview.

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

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

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

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

Right-click the Book line and choose Add Field…

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

Type name <- String() - the field and the value it starts with. Choose OK.

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

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

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

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

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

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

Select the stub, from Book() as pure down to its assignment, and type the constructor the book shows: title in, name set with :=?. Saves when typed.

The EK9 IDE once this is done: Select the stub, from Book() as pure down to its assignment, and type the constructor the book shows

Below it, type the copy constructor: a Book in, its name taken. A generic Shelf will rely on T having exactly this. Saves when typed.

The EK9 IDE once this is done: Below it, type the copy constructor

Right-click the Book line and choose Add Method…

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

title is the method's name. Set Return type to String and tick pure. Choose Apply.

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

Select the stub and type title's answer. Saves when typed.

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

Step 2: Shelf — a generic class

Choose Code > New Construct > Class…

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

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

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

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

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

Select the stub of the one-item constructor and type the book's: first in, the shelf starting with a copy of 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 Shelf line and choose Add Method…

The EK9 IDE once this is done: Right-click the Shelf 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: the item, copied in. Saves when typed.

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

Right-click the Shelf line and choose Add Method…

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

size is the method's name. Set Return type to Integer and tick pure. Choose Apply.

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

Select the stub and type size's answer. Saves when typed.

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

Step 3: The program

Choose Code > New Construct > Program…

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

Library is the program's name. Choose Preview.

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

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

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

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

Step 4: 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.

on the shelf: 2, then first title: Emma. Choose Close.

The EK9 IDE just before choosing Close: on the shelf

The result

Shelf.ek9 now reads exactly as chapter 30 of EK9 in Depth shows it. A generic class must say how it is built both empty and from one T, and the copy it makes of the item - T(item) - is why Book has a copy constructor: the template asks every T for one.

The EK9 IDE when the lesson has finished

Where this lesson comes from