Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Kotlin support "ternary operator" [closed]

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?

like image 991
zhumengzhu Avatar asked Mar 10 '16 07:03

zhumengzhu


People also ask

Why does Kotlin not 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.

Is there ternary operator in Kotlin?

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.

Is it good practice to use ternary operator?

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.

What can I use instead of a ternary operator?

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.


3 Answers

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.

like image 130
dishan Avatar answered Oct 19 '22 13:10

dishan


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" }")
}
like image 39
RoninDev Avatar answered Oct 19 '22 13:10

RoninDev


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

like image 1
voddan Avatar answered Oct 19 '22 12:10

voddan