Ternary Conditional Operator

There is only one ternary conditional operator, the ternary conditional operator (a ? b : c).

It behaves like an if-statement, but is an expression: If the first operator value is true, the second operator value is returned. If the first operator value is false, the third value is returned.

The first value must be a boolean (must have the type Bool). The second value and third value can be of any type. The result type is the least common supertype of the second and third value.

let x = 1 > 2 ? 3 : 4
// `x` is `4` and has type `Int`

let y = 1 > 2 ? nil : 3
// `y` is `3` and has type `Int?`

Last updated