-> (conditional operator)

control structure

Return type: The same as the type of its argument
Returns:       

Syntax
boolean-expression -> if-true-expression | if-false-expression


Purpose

You can use the conditional operator ->...|... to select one of two values based on a condition:

  process
     local integer planet initial {3}
     output "Hello " 
         || (planet = 3 -> "Human" | "Alien")  

The conditional operator has low precedence and so will usually need to be included in parentheses when combined with other operators.

Two conditional expressions can be combined so that one is the argument of another:

  process
     local integer planet initial {3}
     local switch morning initial {false}
     local switch dark-out initial {true}
     
     output "Good " 
         || (morning -> "morning" | dark-out -> "evening" | "afternoon")
         || " "
         || (planet = 3 -> "Human" | "Alien")

If one of the alternatives is a numeric literal and the other is not, the numeric literal will be converted to the other type. Alternatively, you can explicitly cast a value to a new type:

  process
     local integer x
     local integer y initial {77}
     local stream s initial {"234"}
         
     set x to (condition ->  y | integer s)

The type of a conditional expression is the type of its arguments. If a different type is expected, implicit conversion will occur where supported by internal or user defined conversion functions.

If the alternative values are of record types, they may be different types as long as both types are extensions of the same base type.

Related Concepts