The ternary operator

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

#!ek9
defines module site.operators.ternary

  defines program

    Ternary()
      stdout <- Stdout()

      //Setup values
      bird1 as String: String()
      bird2 <- "Duck"

      answerA <- bird1? <- bird1 : bird2
      //answerA would have the value of "Duck" because 'bird1' is not set
      //Note this could have been written as
      answerA := bird1? <- bird1 else bird2
      //Maybe this is less terse but more readable and obvious.

      bird1 := String()
      answerA := bird1? <- bird1 : bird2
      //answerA would still have the value of "Duck" because 'bird1' does not have a meaningful value

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

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

Download operators_ternary.ek9

Output

Duck Goose