+1 to everything here. I think with java tentatively targeting JDK 15 for pattern matching preview that we should revisit this. I have had many occasions where destructuring a Pair or Triple would be useful:
when(Pair(int1, int2)) {
(1, ) → something()
(0, _ ) → somethingElse()
(, 5) → anotherThing()
}
This is clearly contrived but these scenarios pop up and the alternative (nested whens) are much less powerful and more verbose and error prone. It would be a massive boon to how expressive the language is and help motivate other teams in my organization to consider kotlin.
I suppose JetBrains could somehow make the compiler understand pattern matching in a way that compilation would only take longer if the source code contained pattern matching logic.
It would also be nice to have some statistics on how longer a big code base using pattern matching would take to compile, compared to a similar code base without it.
With all that, developers could then decide whether they wanted to sacrifice compilation time for some more development comfort with pattern matching.
One important feature of FP languages is full blown support for pattern matching. Kotlin must learn from Scala in this case and provide pattern matching support. Java 15 anyways is coming with Pattern matching support. So, its time to revisit this.
I think introducing pattern matching would make a strong case for Kotlin when looking at Scala. Currently it is one of the most strong arguments against Kotlin.
However, I see that it would make the language more complex. But the same goes for lambda expressions. And one can see how the OP community adapted this FP feature.
Question of preference I suppose, but I don’t find that very idiomatic. I’d rather use a simple when:
fun fibo(n: Int): Int =
when (n) {
1, 2 -> 1
else -> fibo(n - 1) + fibo(n - 2)
}
But for types, it’s already there, sort of, with function overloading.
… Without destructuration though, I suppose it wouldn’t be as useful as in assignments and conditions. I think it’s available in some languages, for ex. Clojure and Javascript.
Let’s see…
fun sum(Pair<Int, Int>(a, b)): Int = a + b
or
data class Person(val name: String, val age: Int, val ID: String)
fun MutableMap<String, Int>.put(Person(k, v, _)) {
put(k, v)
}
No, I’m not convinced. Maybe there are better use-cases.