An Operator’s Result Type
What about the type produced by the operator? Because either the second or third operand can deliver the operator’s result, there needs to be some kind of compatibility between them. When both have the exact same type, there’s no question at all. But what if they don’t line up? Then the answer lies in implicit conversions. For example:
condition ? 42 : 123L
The resulting type of this conditional operator use will be long because it’s possible to convert implicitly from int to long (without loss of data, that is). Obviously, the rule holds symmetrically if the second and third arguments are swapped:
!condition ? 123L : 42
However, in the face of type hierarchies, the operator does not look for the “least common supertype” ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access