Alternative for java conditional operator

I know that currently there is no conditional operator ?: in Kotlin. Do you plan to add a similar feature to Kotlin? IMHO, this kind of operator is really useful because it is used very often by most java developers.

I suggest a fun extension similar to the one below:

fun <T> Boolean.transform(trueValue: T, falseValue: T): T {
    return if (this) trueValue else falseValue
}

it is even better if we can add a new operator for it.

1 Like

The ternary operator has been discussed multiple times here, please refer to the previous discussions.

The extension function as you suggest won’t work because it always evaluates both the true and the false expressions, unlike the ternary operator. You can implement a working version using lambdas, but it’s not clear that it will be any more readable than the existing if operator (which can also be used as an expression: val f = if (x) a else b).

ah, yeah. I see your point!

PS: I searched only for “conditional operator”, that why I missed the created topic. Sorry! :slight_smile:

That’s why we need macros! (Not serious)