General-purpose question about pattern-matching

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

However, When looking at this document by M. Goetz 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, 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 ?

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

Kotlin fundametally doesn’t do any deconstruction.

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 :slight_smile: It would be a nice addition to the language, I think.