What does the '?' operator do in EK9 and what does 'default operator ?' mean?

← Operators and Expressions · Ref: Q877

Forget null. EK9 has no null. Instead it has three states: absent, unset, and set.

The '?' operator is the isSet check — it returns true when a value has meaningful data:

  name <- "Steve"    // name? returns true (set)
  empty <- String()   // empty? returns false (unset)
  if name?
    stdout.println(name)  // safe — we checked first

For your own classes, add 'default operator ?' at the end of the class. The compiler generates the check from your fields. Or write 'override operator ?' for custom logic.

The ':=?' operator assigns only if the target is currently unset — perfect for defaults:

  config :=? "fallback"  // only assigns if config is unset

This is NOT null coalescing. There is no null. See Q29 for tri-state details. See Q84 for Optional.

Example

defines module qa.operators.isset

  defines class

    UserProfile
      userName <- String()
      email <- String()

      UserProfile()
        ->
          name as String
          addr as String
        userName :=: name
        email :=: addr

      describe()
        <- rtn as String: `${userName} <${email}>`

      default operator ?

  defines program

    IsSetDemo()
      stdout <- Stdout()

      //Set profile — '?' returns true
      profile <- UserProfile("Steve", "steve@example.com")
      if profile?
        stdout.println(profile.describe())

      //Unset string — '?' returns false
      unsetName <- String()
      if not unsetName?
        stdout.println("Name is unset")

      //Guard assignment — only assigns if unset
      unsetName :=? "default name"
      stdout.println(unsetName)
Other ways to ask this
  • How does isSet work in EK9?
  • What is the difference between set and unset in EK9?
  • How do I check if a variable has a value in EK9?

Coming from another language?

EK9 replaces null checks from other languages with the ? suffix operator. Append ? after any variable to check if it holds a value.

Keywords: isset, set, default, tristate, check, unset, operator, question, guard