Explain: This question is more about the design intentions of Kotlin. Many expression languages support both Ternary operator
and if expression
[e.g., Ruby, Groovy.]
First of all, I know Groovy supports both Ternary operator
and Elvis operator
: Ternary operator in Groovy. So I don't think it's a syntax problem.
Then the official documents said:
In Kotlin, if is an expression, i.e. it returns a value. Therefore there is no ternary operator (condition ? then : else), because ordinary if works fine in this role.
And this doesn't convince me. Because Kotlin support Elvis operator
which ordinary if works just fine in that role either.
I think ternary operator
is sometimes better than ordinary if
, though I wonder why doesn't Kotlin just support ternary operator
?