Kotlin equivalent of ternary operator

So, in java we have a ternary operator (?), Which is sometimes useful for easy some value calculated using if-else lines. For example:

myAdapter.setAdapterItems( textToSearch.length == 0 ? noteList : noteList.sublist(0, length-5) ) 

I know the equivalent in kotlin would be:

 myAdapter.setAdapterItems( if(textToSearch.length == 0) noteList else noteList.sublist(0, length-5) ) 

But I just loved the ternary operator in Java, for short expression conditions and when passing values ​​to a method. Is there an equivalent to Kotlin?

+4
ternary-operator kotlin
source share
1 answer

There is no triple operator in Kotlin.

https://kotlinlang.org/docs/reference/control-flow.html

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.

+11
source share

All Articles