Chapter 30: a generic function and a function type, built

Chapter 30's generic functions: larger() of type T compares two values of whatever T is, and transformer() of type T as abstract is a function TYPE - a signature with no body that a dynamic function fills in. If you know Java, this is not a method with a type parameter: a generic function is a named construct, made into a value by naming a type. The IDE writes both signatures - Generic ticked, and as abstract for the contract - and the program's shell; you type the comparison and the program. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-generic-functions.

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

  defines function

    larger() of type T
      ->
        a as T
        b as T
      <- rtn as T: a > b <- a : b

    transformer() of type T as abstract
      -> item as T
      <- rtn as T?

  defines program

    GenericFunctionDemo()
      stdout <- Stdout()

      largerInteger <- larger() of Integer
      stdout.println(`larger of 21 and 42: ${largerInteger(21, 42)}`)

      largerString <- larger() of String
      stdout.println(`larger of pear and apple: ${largerString("pear", "apple")}`)

      shout <- () is transformer of String as function
        rtn: item.upperCase() + "!"
      stdout.println(shout("hello"))

      doubled <- () is transformer of Integer as function
        rtn: item * 2
      stdout.println(`doubled: ${doubled(33)}`)

Step 1: larger — a function

Choose Code > New Construct > Function…

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

larger is the function's name. Tick Generic and give one type parameter, T (no constraint). Add the parameters a of type T and b of type T, set Returns to T. Choose Preview.

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

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

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

Select the larger() of type T line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the larger() of type T line and, above it, type the blank line the book opens its first function with

Select the stub, from <- rtn as T? down to its //AI line, and type larger's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as T? down to its //AI line, and type larger's own return and work, as the book writes them

Step 2: transformer — a function

Choose Code > New Construct > Function…

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

transformer is the function's name. Tick Generic and give one type parameter, T (no constraint). Add one parameter, item of type T, set Returns to T and tick as abstract - a function type: a contract with no body. Choose Preview.

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

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

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

Step 3: The program

Choose Code > New Construct > Program…

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

GenericFunctionDemo is the program's name. Choose Preview.

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

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

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

Select the GenericFunctionDemo() 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 GenericFunctionDemo() 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 GenericFunctionDemo'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 GenericFunctionDemo'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.

larger of 21 and 42: 42, larger of pear and apple: pear, then HELLO! and doubled: 66. Choose Close.

The EK9 IDE just before choosing Close: larger of 21 and 42

The result

GenericFunctions.ek9 now reads exactly as chapter 30 of EK9 in Depth shows it. larger() of Integer and larger() of String are two functions made from one template, each checked when it is made; the two dynamic functions implement the abstract transformer for String and for Integer.

The EK9 IDE when the lesson has finished

Where this lesson comes from