Coalescing operators

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

#!ek9
defines module site.operators.coalescing

  defines program

    Coalescing()
      stdout <- Stdout()

      //Initialise values
      bird1 as String: String()
      bird2 <- "Duck"
      answerA <- bird1 <? bird2
      answerA := bird1 <? bird2
      answerA := bird1 >? bird2
      //In all above examples answerA would have the value of "Duck" because 'bird1' is not set

      //As you can see bird1 is not less than, equal to or greater than bird2 because bird1 not set.

      answerB <- bird1 ?: bird2
      //answerB has the value of "Duck" because 'bird1' is not set

      answerB := bird1 ?: bird2
      //answerB again has the value of "Duck" - '?:' replaces a value that exists but has no meaning yet
      //By the same logic answerB would have the value of "Duck" when the <?, >? operators are used.

      bird1 := "Goose"
      answerC <- bird1 ?: bird2
      //answerC would now have the value of "Goose" because bird1 is set
      answerC := bird1 <? bird2
      //answerC would have the value of "Duck"
      answerC := bird1 >? bird2
      //answerC would have the value of "Goose"

      stdout.println(`${answerA} ${answerB} ${answerC}`)

Download operators_coalescing.ek9

Output

Duck Duck Goose