Ternary operator Please


#1

It wold be nice if Kotlin support Ternary operator. I think it is more simple that use when() or if() conditional.

Java
String username = firstName.isEmpty() ? lastName : firstName;

Kotlin
val username = firstName.isEmpty() ? lastName : firstName

It will be very good for us developers.


#2
https://discuss.kotlinlang.org/t/ternary-operator

Let’s not get into this discussion again, as long as there is nothing new to add :wink:


#3

Already discussed to death in other threads.

tl;dr Ain’t gonna happen. Kotlin provides more readable alternatives:

val username = if(firstName.isEmpty()) lastName else firstName

Or even better in this case:

val username = firstName?.takeUnless { it.isEmpty() } ?: lastName

#4

OMG :disappointed_relieved:


#5

Closed as a duplicate of https://discuss.kotlinlang.org/t/ternary-operator