DEV Community

Discussion on: We don't need a ternary operator

Collapse
 
mnayeem profile image
Nayeem Rahman

What if true_value is an optional?

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

I can't remember what I did in Leaf. Either I triggered a compiler error, or more likely I elided the optional -- you can't have an optional of optional, one was just stripped.

Collapse
 
sanderintveld profile image
Sander in 't Veld

An elision would cause its behavior to deviate from that of a ternary operator, though: if b is null/none then true ? b | 3 would return 3 whereas the ternary operation true ? b : 3 returns null.

That is the true strength of the ternary operator: to choose between two paths before evaluating them. All binary operators always have to evaluate one of their operands first.

Thread Thread
 
mortoray profile image
edA‑qa mort‑ora‑y

That is true. And I think for that reason it'd make sense to raise an error if the true_value is an optional. It'd lead to a potential ambiguity in what is desired.