I'm not sure I'd say that it can't be done. In some languages it could be implemented if the language itself supports optionals. So "bool ? x" could result in an Optional<type(x)>, while "Optional<type(x)> : y" could be the typical implementation of value_or that is found on optionals.
Whether it's a good idea to implement it that way is something I haven't come to an opinion on yet. My only argument is that "can't be decomposed" feels like it's a little to far reaching.
Edit: to add on to this, while it's not really a ternary operator in the sense he seems to be looking for. It it common for assembly to have fma instructions. But this feels like a really weird edge case since x * y + z technically can be decomposed into the 2 separate operators, but the fma instruction itself can't be decomposed without losing precision.
> I'm not sure I'd say that it can't be done. In some languages it could be implemented if the language itself supports optionals. So "bool ? x" could result in an Optional<type(x)>, while "Optional<type(x)> : y" could be the typical implementation of value_or that is found on optionals.
It is tricky because, in
flag ? x : y;
exactly one of x and y gets evaluated.
So, flag ? x would have to evaluate flag first, and only evaluate x if that has a truthy value.
That can be tricky in many languages, for example if the programmer writes
(b ≠ 0) ? a ÷ b : 0;
You’d need lazy evaluation of arguments or something similar.
You and GP are basically describing "bool && x" and "bool || y" (for the languages that return the value instead of strict booleans when evaluating && and ||)
Whether it's a good idea to implement it that way is something I haven't come to an opinion on yet. My only argument is that "can't be decomposed" feels like it's a little to far reaching.
Edit: to add on to this, while it's not really a ternary operator in the sense he seems to be looking for. It it common for assembly to have fma instructions. But this feels like a really weird edge case since x * y + z technically can be decomposed into the 2 separate operators, but the fma instruction itself can't be decomposed without losing precision.