Assign if unset

The complete source of operators_assign_if_unset.ek9, compiled by the EK9 compiler. Shown on operators.html.

#!ek9
defines module site.operators.assignifunset

  defines function

    chooseBirds()
      -> initialBird as String
      stdout <- Stdout()

      //Setup values - bird1 is declared with no value
      bird1 as String?

      bird1 :=? "Goose"
      //bird1 would have the value of "Goose" because 'bird1' has no value yet

      //In contrast.
      bird2 <- initialBird //initialBird has the value "Duck"

      bird2 :=? "Goose"
      //bird2 would have the value of "Duck" because 'bird2' was originally set

      stdout.println(`${bird1} ${bird2}`)

  defines program

    AssignIfUnset()
      chooseBirds("Duck")

Download operators_assign_if_unset.ek9

Output

Goose Duck