# Comparison operators in when expressions

**URL:** <https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180>\
**Category:** Language Design\
**Created:** [April 1, 2025, 8:50am UTC](https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180 "2025-04-01T08:50:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [April 1, 2025, 8:50am UTC](https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180/1 "2025-04-01T08:50:56Z")

</div>

I’m aware that I’m not the first person to bring this up, but all discussions on the matter I’ve found are several years old.

When writing a `when` statement, it’s often the case that we’re dealing with numeric values (or generally: `Comparable<T>`s). However, a `when` case in Kotlin currently cannot use any comparison operators. For example, the following sadly doesn’t work:

```auto
val myInteger: Int = randomInt()
val description = when(myInteger){
   < 0 -> "negative"
   0 -> "zero"
   > 0 -> "positive"
}

```

I’ve seen various workarounds, but they’re all very unsatisfying. Using `in` and `is` is acceptable, but comparison operators somehow are not?

Here are a couple of workarounds (I deem none of them acceptable):

```auto
// problematic: "myInteger" is clearly the subject of the "when" expression, 
// yet the "when" is stated to have no subject.
val description = when {
   myInteger < 0 -> "negative"
   myInteger == 0 -> "zero"
   myInteger > 0 -> "positive"
}

```

```auto
// lipstick on a pig, doesn't solve the underlying problem
val description = myInteger.let {
   when {
       it < 0 -> "negative"
       it == 0 -> "zero"
       it > 0 -> "positive"
   }
}

```

```auto
// this one actually does the trick, but readability is poor
val description = when(myInteger) {
   in Int.MIN_VALUE ..< 0 -> "negative"
   0 -> "zero"
   in 1..Int.MAX_VALUE -> "positive"
}

```

Can we **please** reconsider that it might be a good idea to have comparison operators on `when` subjects?

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [April 2, 2025, 10:29pm UTC](https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180/2 "2025-04-02T22:29:11Z")

</div>

You can create some named ranges to simplify for this case:

```auto
    val Int.Companion.negative get() = MIN_VALUE..<0
    val Int.Companion.positive get() = 1..<MAX_VALUE

    val Int.description get() = when(this) {
        in Int.negative -> "negative"
        in Int.positive -> "positive"
        else -> "zero"
    }

```

But in general, your point is valid

---

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [April 3, 2025, 6:35am UTC](https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180/3 "2025-04-03T06:35:01Z")

</div>

@dalewking that’s a new one, haven’t seen that solution before! 🙂

However, as shown in the opening post, I’d like the exhaustiveness check to work on it as well without using “else”. And I think for this construction it would be very difficult for the compiler to verify that because of the indirection.

---

<div class="post-metadata">

**Author:** ![tango24](https://avatars.discourse-cdn.com/v4/letter/t/74df32/32.png) [@tango24](https://discuss.kotlinlang.org/u/tango24)\
**Post date:** [April 3, 2025, 4:11pm UTC](https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180/4 "2025-04-03T16:11:45Z")

</div>

I guess it could be a parser restriction.

```auto
    when (x) {
        0 -> -1
        < 0
        + 1 -> false
    }

```

could read

```auto
    when (x) {
        0 -> -1 < 0
        +1 -> false
    }

```

or

```auto
    when (x) {
        0 -> -1
        < 0 + 1 -> false
    }

```

Interesting enough, the compiler will accept

```auto
    if (x
        > 2) println(true)

```

although the kotlin grammar says  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/4/4792e9c959db33329f59bf467328f995da4f5648.png)  
thus only allows a LF **after** the comparison operator and will reject the `when` example stated above.

---

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [April 3, 2025, 4:36pm UTC](https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180/5 "2025-04-03T16:36:20Z")

</div>

@tango24 if there is an ambiguity, this wouldn’t be the first place in the kotlin syntax where the presence or absence of a newline character makes a difference. I would happily accept that.

---

<div class="post-metadata">

**Author:** ![jacek.s.gajek](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jacek.s.gajek/32/8517_2.png) [@jacek.s.gajek](https://discuss.kotlinlang.org/u/jacek.s.gajek)\
**Post date:** [April 7, 2025, 9:18am UTC](https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180/6 "2025-04-07T09:18:04Z")

</div>

Another option (a bit Scalaish) is to have sealed classes like “PositiveInt”, “NegativeInt”, “Zero” (or better domain-specific like “StockGrowth”/“TemperatureRaise”/whatever).

Then in your code base you don’t need such “whens” at all because you are guaranteed to have a value which belongs to a specific case. Then use some kind of polymorphism to switch between cases.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [August 30, 2025, 1:13pm UTC](https://discuss.kotlinlang.org/t/comparison-operators-in-when-expressions/30180/7 "2025-08-30T13:13:27Z")

</div>

Actually there is a simpler way for this case

```auto
val description = when(myInteger.sign) {
   -1 -> "negative"
   0 -> "zero"
   1 -> "positive"
}

```

But comparison operators are a valid concern
