Chapter 26: your own function type, built

Three parameters match no row of the library's table, so no Predicate or BiFunction fits - but an abstract function is a function type you write yourself. Chapter 26's OwnFunctionType declares limit() as pure abstract, a concrete clamp that is limit, and apply, which takes any function declared is limit. The IDE writes the signatures, with 'as abstract' and 'is limit' set in the New Function dialog; you type clamp's work and the program. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-own-function-type.

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

  defines function

    limit() as pure abstract
      ->
        n as Integer
        low as Integer
        high as Integer
      <- rtn as Integer?

    clamp() is limit as pure
      ->
        n as Integer
        low as Integer
        high as Integer
      <- rtn as Integer: n

      if n < low
        rtn: low
      else if n > high
        rtn: high

    apply() as pure
      -> rule as limit
      <- rtn as Integer: rule(15, 1, 10)

  defines program

    OwnFunctionType()
      stdout <- Stdout()
      stdout.println($clamp(15, 1, 10))
      stdout.println($apply(clamp))

      held as Any: clamp
      stdout.println(`held as Any is set: ${held?}`)

Step 1: limit — a pure function

Choose Code > New Construct > Function…

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

limit is the function's name. Add the parameters n of type Integer, low of type Integer and high of type Integer, set Returns to Integer, tick pure and as abstract - a function type: a contract with no body. Choose Preview.

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

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

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

Select the limit() as pure abstract 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 limit() as pure abstract line and, above it, type the blank line the book opens its first function with

Step 2: clamp — a pure function

Choose Code > New Construct > Function…

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

clamp is the function's name. Add the parameters n of type Integer, low of type Integer and high of type Integer, set Returns to Integer, type limit into Extends and tick pure. Choose Preview.

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

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

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

Select the stub, from <- rtn as Integer? down to its //AI line, and type clamp'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 Integer? down to its //AI line, and type clamp's own return and work, as the book writes them

Step 3: apply — a pure function

Choose Code > New Construct > Function…

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

apply is the function's name. Add one parameter, rule of type limit, set Returns to Integer and tick pure. Choose Preview.

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

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

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

Select the stub, from <- rtn as Integer? down to its //AI line, and type apply'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 Integer? down to its //AI line, and type apply's own return and work, as the book writes them

Step 4: The program

Choose Code > New Construct > Program…

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

OwnFunctionType is the program's name. Choose Preview.

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

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

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

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

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

10 twice - clamp(15, 1, 10) called directly and through apply - then that the function held as an Any is set: true. Choose Close.

The EK9 IDE just before choosing Close: 10 twice - clamp(15, 1, 10) called directly and through apply - then that the function held as an Any is set

The result

OwnFunctionType.ek9 now reads exactly as chapter 26 of EK9 in Depth shows it. limit is a type with a name, apply takes any function that is limit, and the last lines show every function can still be held as an Any - set, though an Any cannot be called.

The EK9 IDE when the lesson has finished

Where this lesson comes from