Why Kotlin does not support the "triple operator",

Explain that this issue is more related to Kotlin’s design intentions. Many expression languages ​​support both the Ternary operator and if expression [for example, Ruby, Groovy.]


First of all, I know that Groovy supports both the Ternary operator and the Elvis operator : The ternary operator in Groovy . Therefore, I do not think this is a syntax problem.


Then the official docs said:

In Kotlin, if is an expression, i.e. returns the value. Therefore, there is no triple operator (condition? Then: else), because it is normal if it works perfectly in this role.

And this does not convince me. Because Kotlin supports the Elvis operator , which is normal if it works perfectly in this role.

I think the Ternary operator sometimes better than the usual if , although I wonder why Kotlin just doesn't support the Ternary operator ?

+10
ternary-operator kotlin
source share
4 answers

In languages ​​that have a ternary operator , you use it like this

 String value = condition ? foo : bar; 

In Kotlin, you can do the same using if and else

 var value = if(condition) foo else bar; 

Its a bit verbose than a ternary operator . But the designers of Kotlin thought that everything was in order. You can use if-else like this because in Kotlin if there is an expression and returns a value

Elvis operator is essentially a compressed version of the ternary conditional operator and is equivalent to the following in Kotlin.

 var value = if(foo != null) foo else bar; 

But if the Elvis operator , it is simplified as follows:

 var value = foo ?: bar; 

This is a significant simplification, and Kotlin decided to keep it.

+5
source share

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" }") } 
+4
source share

The ternary operator has its own problems, for example, it is difficult to read with large expressions. Here is a line from my C ++ project where I used the ternary operator:

 const long offset = (comm_rank > 0) ? task_size_mod + (comm_rank - 1) * task_size : 0; 

I would prefer to use the if else expression here, as it is much more noticeable.

Answering the question, I know two reasons why the ternary operator not implemented in Kotlin:

1) Since if else is an expression, can it replace ? : ? :

2) The experience of other languages ​​(C ++) shows that ? : ? : calls hard-to-read code, so it’s better not to consider

+1
source share

As you said, in Kotlin there is not a single ternary operator that would have a regular look (as in other popular languages), which we are used to:

 condition ? then : else 

This is because in Kotlin, the usual if - else work great in this role.

Example 1:

 val a = 5 val b = 2 if (a > b) println(a) else println(b) // Result: 5 

Kotlin designers have provided you with similar functionality, but with richer potential.

Example 2:

 val a = 2 val b = 2 if (a > b) { println(a) } else if (a == b) { println("They are equal") } else { println(b) } // Result: "They are equal" 

And of course, Elvis Operator can be used in more elegant nullable designs.

Example 3:

 fun maximum(a: Int?, b: Int) = a ?: b println(maximum(50,7)) // Result: 50 

or if a is null

 fun maximum(a: Int?, b: Int) = a ?: b println(maximum(null,7)) // Result: 7 

Hope this helps.

-one
source share

All Articles