Unset values, set checks and guards

The complete source of builtInTypes_tri_state.ek9, compiled by the EK9 compiler. Shown on builtInTypes.html.

#!ek9
defines module site.builtInTypes.triState

  defines function

    process()
      -> name as String
      Stdout().println(`Processing ${name}`)

    getName()
      <- rtn as String: "Steve"

  defines program

    TriStateExample()
      //Creating an unset value (NOT null)
      name <- String()      //Unset String
      require not name?     //_isSet() returns false

      //Checking if set before use
      chosen <- getName()
      if chosen?
        process(chosen)     //Safe - guaranteed set

      //Guard for safe access
      if validName <- getName()
        process(validName)  //Safe - guaranteed set

Download builtInTypes_tri_state.ek9

Output

Processing Steve
Processing Steve