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.
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…
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 preview shows larger about to be written. Choose Apply.
Select the larger() of type T line and, above it, type the blank line the book opens its first function with. Saves when typed.
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.
Step 2: transformer — a function
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 preview shows transformer about to be written. Choose Apply.
Step 3: The program
Choose Code > New Construct > Program…
GenericFunctionDemo is the program's name. Choose Preview.
The preview shows GenericFunctionDemo about to be written. Choose Apply.
Select the GenericFunctionDemo() line and, above it, type the blank line the book opens the program with. Saves when typed.
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.
Step 4: Run it
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 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.
Where this lesson comes from
- Answers -
ek9 -qand the IDE's Answers tab: 58. - For AI assistants - the MCP tool
ek9_new_construct,ek9_add_program.
















