# Reduced implementation of conditions in "if" and "when" expressions

**URL:** <https://discuss.kotlinlang.org/t/reduced-implementation-of-conditions-in-if-and-when-expressions/14929>\
**Category:** Language Design\
**Created:** [November 7, 2019, 10:52am UTC](https://discuss.kotlinlang.org/t/reduced-implementation-of-conditions-in-if-and-when-expressions/14929 "2019-11-07T10:52:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ScarletPhoenix7526](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@ScarletPhoenix7526](https://discuss.kotlinlang.org/u/ScarletPhoenix7526)\
**Post date:** [November 7, 2019, 10:52am UTC](https://discuss.kotlinlang.org/t/reduced-implementation-of-conditions-in-if-and-when-expressions/14929/1 "2019-11-07T10:52:04Z")

</div>

Is there an implementation of this type (or similar)?

> when {  
> firstVal == _1_ || _2_ **→** some code  
> secondVal == _“a”_ && _“b”_ **→** some code  
> }

Otherwise, I would not want to write:

> if (firstVal == _1_ || firstVal == _2_) {  
> some code  
> }

If this is not, is it possible to add it to the language?

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [November 7, 2019, 11:25am UTC](https://discuss.kotlinlang.org/t/reduced-implementation-of-conditions-in-if-and-when-expressions/14929/2 "2019-11-07T11:25:43Z")

</div>

You can consider

```
when (firstVal) {
    1, 2 -> TODO()
    else -> when (secondVal) {
        "a", "b" -> TODO()
    }
}

if (firstVal in 1..2) {
    TODO()
}

if (firstVal in intArrayOf(1, 2)) {
    TODO()
}

```

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [November 8, 2019, 10:06am UTC](https://discuss.kotlinlang.org/t/reduced-implementation-of-conditions-in-if-and-when-expressions/14929/3 "2019-11-08T10:06:23Z")

</div>

> [@fvasco](#):
>
> when (firstVal) { 1, 2 → TODO() else → when (secondVal) { “a”, “b” → TODO() } } if (firstVal in 1…2) { TODO() } if (firstVal in intArrayOf(1, 2)) { TODO() }

Or just:

```auto
when {
    firstVal in 1..2 -> TODO()
    secondVal == "a" && 
        secondVal.someOtherProp =="b" -> TODO()
}

```

If you don’t specify a variable on a when it just becomes a sequentially evaluated set of rules where the first satisfied condition matches and terminates the evaluation. Basically equivalent to an `if(...) {...} else if (...) {...} else if (...) {...} else {...}` chain.
