Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: