I’m wondering if anyone know if it’s possible to s...
# arrow
p
I’m wondering if anyone know if it’s possible to somehow “pattern match” based on generic type without nasty unchecked casts etc, eg: https://gist.github.com/peterfigure/4e587895ed56b0f0c4b1d59c45613608 Is there anything in Arrow that can help?
r
Hi Peter, nothing at the moment, inline reified and use of typeOf aside, Kotlin in general can’t unify in pattern matching the contemplated cases when generics are involved like this. I believe it should work too but here Kotlin pattern matching falls short of what you’d expect to happen when the type is bound by a sealed hierarchy and this happens even without reflection in the most simple cases
Copy code
sealed class A()
object B: A()

fun <T : A> t(t: T): String =
  when (t) {
    is B -> "all cases covered"
    else -> TODO("this case is needed because Kotlin does " +
      "not account for type bounds in the pattern " +
      "match ensuring T is is a subtype of A and A being sealed B is all its needed")
  }
😭 1
I feel you, I’ve dragged this corner cases where we end up casting all over my soul for years. I hope pattern matching in Kotlin gets better type inference support soon too.
p
thank you @raulraja - i know you guys have a big voice in the community, hopefully inspire roman & co to evolve on the language 🙏
r
I brought this up a few times in different conversations but I think there is no easy solution to some of the pattern matching requests they get from all over the place given what Java proposes and in a way makes Kotlin wait to see what happens with all those features https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html
to be honest I dislike those casts but also understand they are focused in other areas that are also important and yet hopeful we will get better pattern matching support at some point
🤞 1
In whatever case I think we need to probably create an issue and gather our thoughts around the problems of pattern matching and generics. I searched but could not find one in the tracker.
p
that’s a good idea