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.
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…
orUnset is the function's name. Add one parameter, text of type String, set Returns to String and tick pure. Choose Preview.
The preview shows orUnset about to be written. Choose Apply.
Select the orUnset() as pure line and, above it, type the blank line the book opens its first function with. Saves when typed.
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.
Step 2: The program
Choose Code > New Construct > Program…
FourStates is the program's name. Choose Preview.
The preview shows FourStates about to be written. Choose Apply.
Select the FourStates() 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 FourStates's body exactly as the chapter shows it. Saves when typed.
Step 3: Run it
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 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.
Where this lesson comes from
- For AI assistants - the MCP tool
ek9_new_construct,ek9_add_program.













