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
?
There's certainly a temptation to create a DSL that mimics a ternary operator. But, Kotlin's language restrictions are too tight to allow for a 100% reproduction of the traditional ternary syntax. As such, let's avoid this temptation and simply use one of the earlier mentioned solutions.
Kotlin if-else expression as ternary operator: Unlike java, there is no ternary operator in Kotlin because if-else returns the value according to the condition and works exactly similar to ternary. Below is the Kotlin program to find the greater value between two numbers using if-else expression.
The conditional ternary operator can definitely be overused, and some find it quite unreadable. However, I find that it can be very clean in most situations that a boolean expression is expected, provided that its intent is clear.
The alternative to the ternary operation is to use the && (AND) operation. Because the AND operator will short-circuit if the left-operand is falsey, it acts identically to the first part of the ternary operator.
In languages which have ternary operator
you use it like this
String value = condition ? foo : bar;
In Kotlin you can do the same thing using if and else
var value = if(condition) foo else bar;
Its bit verbose than the ternary operator
. But designers of Kotlin have thought it is ok. You can use if-else
like this because in Kotlin if
is an expression and returns a value
Elvis operator
is essentially a compressed version of ternary conditional statement and equivalent to following in Kotlin.
var value = if(foo != null) foo else bar;
But if Elvis operator
is used it simplify as follows
var value = foo ?: bar;
This is considerable simplification and Kotlin decided to keep it.
Because if .. else ..
works fine. Take a look:
fun main(args: Array<String>) {
var i = 2
println("i ${ if(i == 1) "equals 1" else "not equals 1" }")
}
Ternary operator has its problems, for example it is hard to read with big expressions. Here is a line from my C++ project where I used ternary operator:
const long offset = (comm_rank > 0) ? task_size_mod + (comm_rank - 1) * task_size : 0;
I would rather use an if else
expression here since it is so much more visible.
Answering you question, I am aware of two reasons why ternary operator
was not implemented in Kotlin:
1) Since if else
is an expression anyway, it can replace ? :
2) Experience from other languages (C++) shows that ? :
provokes hard-to-read code, so it is better to be left out
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With