Chapter 24: the four states of a Result, built

If you know Rust, a Result is Ok or Err and 'not Ok' means Err. In EK9 a Result has four states - neither side set, ok only, error only, or both - so the else of isOk() proves nothing about error(). This program builds all four and prints what each reports; a small pure helper, orUnset, makes an unset String visible. The IDE writes the helper's signature and the program's shell; you type the rest. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-result-states.

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

  defines function

    orUnset() as pure
      -> text as String
      <- rtn as String: text? <- text else "(unset String)"

  defines program

    FourStates()
      stdout <- Stdout()

      neither <- Result() of (Integer, String)
      okOnly <- Result(42, String())
      errorOnly <- Result(Integer(), "no answer")
      both <- Result(42, "answered, with a caveat")

      for outcome in [neither, okOnly, errorOnly, both]
        stdout.println(`set ${outcome?} ok ${outcome.isOk()} error ${outcome.isError()} printed ${orUnset($outcome)}`)

      nothing <- Optional() of String
      something <- Optional("present")
      stdout.println(`empty ${orUnset($nothing)} full ${orUnset($something)} set ${nothing?} ${something?}`)

      stdout.println("before")
      stdout.println($neither)
      stdout.println(`[${neither}]`)
      stdout.println("after")

Step 1: orUnset — a pure function

Choose Code > New Construct > Function…

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

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

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

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

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

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

Select the stub, from <- rtn as String? down to its //AI line, and type orUnset'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 String? down to its //AI line, and type orUnset'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…

FourStates is the program's name. Choose Preview.

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

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

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

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

Four lines for the four states, one line for the empty Optional against the full one, then 'before' and 'after' with nothing between them - the two prints of an unset String wrote no line at all. Choose Close.

The EK9 IDE just before choosing Close: Four lines for the four states, one line for the empty Optional against the full one, then 'before' and 'after' with nothing between them - the two prints of an unset String wrote no line at all

The result

ResultStates.ek9 now reads exactly as chapter 24 of EK9 in Depth shows it. The first four lines are the four states, and the 'before' and 'after' lines show the cost: printing an unset String prints nothing at all, not even a newline.

The EK9 IDE when the lesson has finished

Where this lesson comes from