Chapter 30: a range over a type parameter, built

A range over a type parameter: countFromTo() of type T counts the steps from one T to another with a for ... in from ... to loop. A range needs <=>, a copy constructor and ++ on the type, so the template is checked for each T it is made for - Integer, Character and Date work; String, which has no ++, would be refused. The IDE writes the signature (Generic ticked) and the program's shell; you type the loop and the three uses. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-range-over-t.

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

  defines function

    countFromTo() of type T
      ->
        from as T
        to as T
      <- rtn as Integer: 0

      for step in from ... to
        rtn++

  defines program

    RangeOverT()
      stdout <- Stdout()
      countIntegers <- countFromTo() of Integer
      stdout.println(`1 to 5: ${countIntegers(1, 5)}`)
      countCharacters <- countFromTo() of Character
      stdout.println(`a to e: ${countCharacters('a', 'e')}`)
      countDates <- countFromTo() of Date
      stdout.println(`March 2026: ${countDates(2026-03-01, 2026-03-31)}`)

Step 1: countFromTo — a function

Choose Code > New Construct > Function…

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

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

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

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

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

Select the countFromTo() 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 countFromTo() of type T line and, above it, type the blank line the book opens its first function with

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

Step 2: The program

Choose Code > New Construct > Program…

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

RangeOverT is the program's name. Choose Preview.

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

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

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

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

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

1 to 5: 5, a to e: 5 and March 2026: 31. Choose Close.

The EK9 IDE just before choosing Close: 1 to 5

The result

RangeOverT.ek9 now reads exactly as chapter 30 of EK9 in Depth shows it. One template counted integers, characters and days - a range that needs only the operators the loop uses, checked where T is filled in, not at the definition.

The EK9 IDE when the lesson has finished

Where this lesson comes from