# Alternative for java conditional operator

**URL:** <https://discuss.kotlinlang.org/t/alternative-for-java-conditional-operator/2138>\
**Category:** Language Design\
**Created:** [December 6, 2016, 9:06pm UTC](https://discuss.kotlinlang.org/t/alternative-for-java-conditional-operator/2138 "2016-12-06T21:06:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nguyen-sde](https://avatars.discourse-cdn.com/v4/letter/n/fbc32d/32.png) [@nguyen-sde](https://discuss.kotlinlang.org/u/nguyen-sde)\
**Post date:** [December 6, 2016, 9:06pm UTC](https://discuss.kotlinlang.org/t/alternative-for-java-conditional-operator/2138/1 "2016-12-06T21:06:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [December 6, 2016, 9:16pm UTC](https://discuss.kotlinlang.org/t/alternative-for-java-conditional-operator/2138/2 "2016-12-06T21:16:03Z")

</div>

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`).

---

<div class="post-metadata">

**Author:** ![nguyen-sde](https://avatars.discourse-cdn.com/v4/letter/n/fbc32d/32.png) [@nguyen-sde](https://discuss.kotlinlang.org/u/nguyen-sde)\
**Post date:** [December 6, 2016, 11:18pm UTC](https://discuss.kotlinlang.org/t/alternative-for-java-conditional-operator/2138/3 "2016-12-06T23:18:57Z")

</div>

ah, yeah. I see your point!

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

---

<div class="post-metadata">

**Author:** ![jacobz\_20](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jacobz_20/32/1468_2.png) [@jacobz\_20](https://discuss.kotlinlang.org/u/jacobz_20)\
**Post date:** [December 8, 2016, 6:44pm UTC](https://discuss.kotlinlang.org/t/alternative-for-java-conditional-operator/2138/4 "2016-12-08T18:44:23Z")

</div>

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