Chapter 41: the tri-state operators, built

Chapter 41 argues that EK9 has no null, because a value is absent, present but unset, or present and set, and the program asks about the last two with ?. Two small functions make a set value or an unset one on demand, and three programs use the tri-state operators: :=? assigns only when the target is unset, ?: supplies a fallback, and an unset operand makes an arithmetic result unset without a word. The IDE writes the constants, both signatures and every program's shell; you type the answers and the logic. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-tri-state.

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.payoffs.tristate

  defines constant
    FALLBACK_NAME <- "anon"

    OFFSET <- 5

  defines function

    nameOf() as pure
      -> known as Boolean
      <- rtn as String: known <- "Ada" : String()

    numberOf() as pure
      -> known as Boolean
      <- rtn as Integer: known <- 42 : Integer()

  defines program

    AssignIfUnset()
      stdout <- Stdout()
      name <- nameOf(true)
      name :=? FALLBACK_NAME
      stdout.println(`a set name stays [${name}]`)
      missing <- nameOf(false)
      missing :=? FALLBACK_NAME
      stdout.println(`an unset name becomes [${missing}]`)

    Fallback()
      stdout <- Stdout()
      unknown <- numberOf(false)
      chosen <- unknown ?: OFFSET
      stdout.println(`unset or ${OFFSET} gives ${chosen}, set? ${chosen?}`)
      known <- numberOf(true)
      kept <- known ?: OFFSET
      stdout.println(`set or ${OFFSET} gives ${kept}, set? ${kept?}`)

    UnsetFlows()
      stdout <- Stdout()
      unknown <- numberOf(false)
      total <- unknown + OFFSET
      stdout.println(`total is [${total}]`)
      stdout.println(`total set? ${total?}`)
      stdout.println($total)
      stdout.println("after the unset lines")

//EOF

Step 1: Constants

Choose Code > New Construct > Constant…

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

FALLBACK_NAME is the constant's name and "anon" its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: FALLBACK_NAME is the constant's name and "anon" its value - a literal in the Value field, from which the type is inferred

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

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

Choose Code > New Construct > Constant…

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

OFFSET is the constant's name and 5 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: OFFSET is the constant's name and 5 its value - a literal in the Value field, from which the type is inferred

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

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

Step 2: nameOf — a pure function

Choose Code > New Construct > Function…

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

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

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

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

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

Select the nameOf() 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 nameOf() 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 nameOf'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 nameOf's own return and work, as the book writes them

Step 3: numberOf — a pure function

Choose Code > New Construct > Function…

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

numberOf is the function's name. Add one parameter, known of type Boolean, set Returns to Integer and tick pure. Choose Preview.

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

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

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

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

Step 4: The program

Choose Code > New Construct > Program…

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

AssignIfUnset is the program's name. Choose Preview.

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

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

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

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

Step 5: Fallback — another program

Choose Code > New Construct > Program…

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

Fallback is the program's name. Choose Preview.

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

The preview shows Fallback about to be written after the program before it. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Fallback about to be written after the program before it

Select the Fallback() line and, above it, type the blank line the book keeps between programs. Saves when typed.

The EK9 IDE once this is done: Select the Fallback() line and, above it, type the blank line the book keeps between programs

Select the stub, from ready <- true down to its //AI line, and type Fallback'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 Fallback's body exactly as the chapter shows it

Step 6: UnsetFlows — another program

Choose Code > New Construct > Program…

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

UnsetFlows is the program's name. Choose Preview.

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

The preview shows UnsetFlows about to be written after the program before it. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows UnsetFlows about to be written after the program before it

Select the UnsetFlows() line and, above it, type the blank line the book keeps between programs. Saves when typed.

The EK9 IDE once this is done: Select the UnsetFlows() line and, above it, type the blank line the book keeps between programs

Select the stub, from ready <- true down to its //AI line, and type UnsetFlows'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 UnsetFlows's body exactly as the chapter shows it

Step 7: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

Choose AssignIfUnset from the list, then choose Run.

a set name stays [Ada], and an unset name becomes [anon]. Choose Close.

The EK9 IDE just before choosing Close: a set name stays (Ada), and an unset name becomes (anon)

Step 8: Run Fallback

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

Choose Fallback from the list, then choose Run.

The EK9 IDE just before choosing Run: Choose Fallback from the list, then choose Run

unset or 5 gives 5, set? true; then set or 5 gives 42, set? true. Choose Close.

The EK9 IDE just before choosing Close: unset or 5 gives 5, set? true; then set or 5 gives 42, set? true

Step 9: Run UnsetFlows

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

Choose UnsetFlows from the list, then choose Run.

The EK9 IDE just before choosing Run: Choose UnsetFlows from the list, then choose Run

only two lines: total set? false, and after the unset lines - the interpolated string and the printed $total were unset, so they printed nothing. Choose Close.

The EK9 IDE just before choosing Close: only two lines

The result

TriState.ek9 now reads exactly as chapter 41 of EK9 in Depth shows it. Three programs, one lesson each: :=? never overwrites a real value, ?: gives its right side only when its left is unset, and the sum with an unset operand is unset - which prints nothing at all, only total? says so.

The EK9 IDE when the lesson has finished

Where this lesson comes from