How does EK9 handle the concept of 'no value' without null?

← Getting Started · Ref: Q883

There is no null in EK9. Not deprecated, not hidden — it does not exist in the language.

Instead, every value has three possible states:

1. ABSENT — the variable does not exist (e.g., missing Dict key)
2. UNSET — the variable exists but has no meaningful data yet
3. SET — the variable has valid, usable data

You create UNSET values with the type constructor and no arguments:

  name <- String()     // exists but unset — name? returns false
  count <- Integer()   // exists but unset — count? returns false

You create SET values by providing data:

  name <- "Steve"      // set — name? returns true
  count <- 42          // set — count? returns true

Collections are different — they are SET even when empty:

  items <- List() of String   // SET (empty list is a valid list)

Always check with '?' before using a value that might be unset:

  if name?
    stdout.println(name)    // safe

See Q29 for full tri-state details. See Q84 for Optional type. See Q877 for the ? operator.

Example

defines module qa.getting.started.tristate

  defines constant

    KNOWN_USER_ID <- 42

  defines function

    findUser() as pure
      -> userId as Integer
      <- rtn as String: String()

      //Returns unset String if user not found
      if userId == KNOWN_USER_ID
        rtn: "Steve"

  defines program

    TriStateDemo()
      stdout <- Stdout()

      //Set value
      known <- findUser(KNOWN_USER_ID)
      if known?
        stdout.println(`Found: ${known}`)

      //Unset value
      unknown <- findUser(99)
      if not unknown?
        stdout.println("User not found — but no null, no exception")

      //Unset with guard assignment
      fallback <- String()
      fallback :=? "anonymous"
      stdout.println(`Fallback: ${fallback}`)
Other ways to ask this
  • EK9 has no null — so what do I use instead?
  • What is the tri-state model in EK9?
  • How do absent, unset, and set work in EK9?

Coming from another language?

Java: null + NullPointerException. Python: None. Rust: Option<T>. Kotlin: nullable types. EK9: tri-state (absent/unset/set) with ? operator — NullPointerException is structurally impossible.

Keywords: null, tristate, set, optional, safe, unset, absent, isset