18

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?

Salah Alshaal
  • 860
  • 2
  • 17
  • 41
zhumengzhu
  • 698
  • 5
  • 22
  • 1
    Possible duplicate of [Kotlin Ternary Conditional Operator](http://stackoverflow.com/questions/16336500/kotlin-ternary-conditional-operator) – Jayson Minard Mar 11 '16 at 14:17
  • Also duplicate of the duplicate: http://stackoverflow.com/questions/34935918/kotlin-equivalent-of-ternary-operator – Jayson Minard Mar 11 '16 at 14:17

5 Answers5

4

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.

dishan
  • 1,346
  • 12
  • 21
2

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" }")
}
RoninDev
  • 5,446
  • 3
  • 23
  • 37
  • 6
    `if-else` works fine in many languages and they have ternary operator too ([look at Ruby](https://gist.github.com/maxd/76e924d147d60cdc003c) for example). – Maxim Mar 10 '16 at 08:38
  • I just wrote that Andrey Breslav (one of the authors of Kotlin) said in his video presentations in russian. i.e. "if else is fine, no needs of ternary operator" – RoninDev Mar 10 '16 at 08:45
  • 2
    Right, but this is very doubtful argument (like extension methods is useless because exists "utility" classes but extension methods is exists in Kotlin). Except this argument I don't see real motivation why they can't implement ternary operator because ternary operators is a syntax sugar like extension methods. – Maxim Mar 10 '16 at 08:56
  • Are the referenced video presentations in Russian available online? If so, where can I find them? Thanks. – mfulton26 Mar 10 '16 at 16:23
  • I saw video from this post https://habrahabr.ru/company/jugru/blog/278647/ I'm not sure about which one of them contains phrase about ternary operator – RoninDev Mar 11 '16 at 05:50
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

voddan
  • 31,956
  • 8
  • 77
  • 87
  • 6
    Here is `if-else` instead of ternary operator: `const long offset = if (comm_rank > 0) task_size_mod + (comm_rank - 1) * task_size else 0;` - it is still a big expression :) Just need to use ternary operators for simple conditions and `if-else` (in multi-line variant) in other cases (as in your case). Moreover, in some cases ternary operator has better "readability" than `if-else` because it shorter. – Maxim Mar 10 '16 at 09:07
  • @maxd If I had `if-else` here I would split it into 4 lines. For `? :` it is considered a nonsense – voddan Mar 10 '16 at 10:12
-1

Programmers at some point, have to make a decision to execute a block of code, this is known as Control Flow. If statement is most basic way to control flow in Kotlin. Important point to note that in Kotlin, If is an expression not a statement as it is Java.

  1. Statement: A statement is an executed line which does not return a value. As a result, statement cannot sit right side of an equal sign.
  2. Expression: An expression returns a value.So the result of a Kolin If expression can be assigned to a variable.

    Because of this the ternary expression would be redundant and does not exists in Kotlin. In Java we would write (Here is the answer of your question)

For Example

Ternary Operator in Java

int lowest = (a < b) ? a : b;

In Kotlin we can write something similar using the if expression.

val lowest = if(a < b) a else b

NOTE: When If is used as an expression it must contain an else clause. The expression must have a value in all case.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
-3

Because if-else is an expression in Kotlin :

String check = number % 2 == 0 ? "even" : "odd" // Java

if (number % 2 == 0) "even else "odd" // Kotlin

So that's why there are no ternary operator in Kotlin, Moreover you can use when expressions too, it's so handy if you want to provide a lot of possible execution paths

Kelvin M
  • 67
  • 5