Chapter 14: an enumeration, built

Chapter 14 makes an enumeration: Day, a fixed set of named values that comes with printing, comparing and build-from-text for free - then a kindOf function that switches on it, and a Week program that prints every day. The IDE's own tools make the structure - the enumeration and its seven values in one call, each function's whole signature - so what is left to type is only the logic no tool can write: what kindOf decides, what Week prints. To play it in the IDE: Help > Lessons > Writing code…, or ek9 -ide -lesson book-enumerations.

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

  defines type
    Day
      Monday
      Tuesday
      Wednesday
      Thursday
      Friday
      Saturday
      Sunday

  defines function
    kindOf() as pure
      -> day as Day
      <- rtn as String: "weekday"
      switch day
        case Day.Monday, Day.Tuesday, Day.Wednesday, Day.Thursday, Day.Friday
          rtn: "weekday"
        case Day.Saturday, Day.Sunday
          rtn: "weekend"
        default
          rtn: "no such day"

  defines program
    Week()
      stdout <- Stdout()
      for day in Day
        stdout.println(`${day} is a ${kindOf(day)}`)

Step 1: An enumeration

Choose Code > New Construct > Type > Enumeration…

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

Day is the enumeration's name. Add each of the seven days as a value, in order - Monday through Sunday - then choose Preview.

The EK9 IDE just before choosing Preview: Day is the enumeration's name

The preview shows Day about to be written, all seven values together; then the IDE formats the file it writes. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Day about to be written, all seven values together; then the IDE formats the file it writes

Step 2: kindOf — and its logic

Choose Code > New Construct > Function…

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

kindOf is the function's name. Add one parameter, day of type Day, set Returns to String, and tick pure. Choose Preview.

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

The preview shows kindOf about to be written, pure, day in and String out. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows kindOf about to be written, pure, day in and String out

Select the stub, from <- rtn as String? down to its //AI line, and type kindOf's decision: the real return and a switch over every day with a default for the rest. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String? down to its //AI line, and type kindOf's decision

Step 3: The program — and its logic

Choose Code > New Construct > Program…

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

Week is the program's name - no application to bind, no parameters, nothing injected. Type Week and choose Preview.

The EK9 IDE just before choosing Preview: Week is the program's name - no application to bind, no parameters, nothing injected

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

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

Select the stub, from ready <- true down to its //AI line, and type Week's body: a Stdout, and a line for every day printing what kindOf makes of it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type Week's body

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.

Monday is a weekday ... Saturday is a weekend ... Sunday is a weekend - kindOf answers for every day, and the switch never had to guess. Choose Close.

The EK9 IDE just before choosing Close: Monday is a weekday ..

The result

Days.ek9 now reads exactly as chapter 14 of Getting Started with EK9 shows it: an enumeration the tools built in one call, with the function and program that use it, logic you typed. The same three tools - ek9_new_construct (twice: 'type'/'enumeration' and 'function') and ek9_add_program - are what an AI assistant calls to do the same thing.

The EK9 IDE when the lesson has finished

Where this lesson comes from