# General-purpose question about pattern-matching

**URL:** <https://discuss.kotlinlang.org/t/general-purpose-question-about-pattern-matching/18245>\
**Category:** Language Design\
**Created:** [June 26, 2020, 12:36pm UTC](https://discuss.kotlinlang.org/t/general-purpose-question-about-pattern-matching/18245 "2020-06-26T12:36:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexis.manin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexis.manin/32/8381_2.png) [@alexis.manin](https://discuss.kotlinlang.org/u/alexis.manin)\
**Post date:** [June 26, 2020, 12:36pm UTC](https://discuss.kotlinlang.org/t/general-purpose-question-about-pattern-matching/18245/1 "2020-06-26T12:36:44Z")

</div>

Hi all,

I’m really confused about the pattern-matching concept. I read in mutiple places that kotlin does not support pattern matching (last example I’ve read is this [Arrow issue](https://github.com/arrow-kt/arrow/issues/1470)).

However, When looking at [this document by M. Goetz](https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html) talking about possible pattern matching in Java, It looks to me that Kotlin already supports all of the described patterns.

If I look at [Scala documentation](https://docs.scala-lang.org/tour/pattern-matching.html), it looks like only pattern guards are not supported currently.

So, I don’t understand what “Kotlin does not support pattern matching” statement means. Can someone explain it to me please ? Does it mean "some kind of advanced " pattern matching is not supported, as [this keep proposal](https://github.com/Kotlin/KEEP/pull/213) ?

Any examples of pattern-matching known limitations in Kotlin, and solutions provided by other languages are welcome 🙂

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [June 26, 2020, 12:49pm UTC](https://discuss.kotlinlang.org/t/general-purpose-question-about-pattern-matching/18245/2 "2020-06-26T12:49:07Z")

</div>

Kotlin fundametally doesn’t do any deconstruction.

---

<div class="post-metadata">

**Author:** ![mtimmerm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mtimmerm/32/7160_2.png) [@mtimmerm](https://discuss.kotlinlang.org/u/mtimmerm)\
**Post date:** [June 26, 2020, 1:25pm UTC](https://discuss.kotlinlang.org/t/general-purpose-question-about-pattern-matching/18245/3 "2020-06-26T13:25:29Z")

</div>

Most of the examples that Goetz shows are actually not supported in Kotlin at all. I think you’re mistaking how they’re supposed to work.

For example, some red-black tree code with pattern matching might look like:

```
when(n) {
    Node(RED, Node(RED,a,b), c) -> {...}
    ...

```

This does not construct a bunch of temporary objects and use `==`. It would mean:

```
when {
    (n.component1() == RED && n.component2()?.component1() == RED) -> {
        val a = n.component2().component2()
        val b = n.component2().component3()
        val c = n.component3()
        ...
    }
...
}

```

This kind of thing is actually extremely useful when you’re implementing a red-black tree 🙂 It would be a nice addition to the language, I think.

---

<div class="post-metadata">

**Author:** ![computadorgranpiador](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@computadorgranpiador](https://discuss.kotlinlang.org/u/computadorgranpiador)\
**Post date:** [June 21, 2024, 8:06am UTC](https://discuss.kotlinlang.org/t/general-purpose-question-about-pattern-matching/18245/4 "2024-06-21T08:06:53Z")

</div>

I would prefer:

```kotlin
when (myVar) {
  Client(val name, ActiveMembership(PlatinumMember(var id, val membershipStartDate))) -> incrementDate(id, membershipStartDate)
  val Client(name, ActiveMembership(RegularMembership(id))) -> updateSettings(id)
  Client(val name, Expired(val anyMembershipType)) -> sendWarning(anyMembershipType)
  VipClinet(val name, val membershipStatus) -> freeRenewal(membershipStatus)
}

```
