Hacker News new | ask | show | jobs
by pistoleer 583 days ago
After some thinking, the ternary conditional operator can be decomposed into 2 composing binary operators like such:

? takes a bool, a T, and returns option<T>

true?b == Result b

false?b == None

: takes an Option<T> and a T and returns T

Result x : y == x

None : y == y

However, in most languages (looking at you php) the ?: act as a type of parenthesis: in a?b:c, any expression goes into b, no matter it's precedence.

2 comments

Nice. Your : operator is roughly the C# null-coalescing operator ??.

    x ?? y == x    // when x is not null

    null ?? y == y
Nice. Another aspect of the ternary operator is conditional evaluation. Beyond parenthesis, in a?b:c, only one of b and c get evaluated.