I am probably overlooking something but I'm wondering the following. Using TypeTags in Scala 2.13 I can do this:
def fun[T: TypeTag](v: List[T]) = {
v match {
case v2: List if typeOf[T] =:= typeOf[Option[Int]] => {
val v3 = v2.asInstanceOf[List[Option[Int]]]
v3.map(x: Int => ...)
}
...
}
}
In this example I can use v3 with the correct type and I can be sure that the cast succeeds since I checked it before via the TypeTag. But it feels broken since I have to enter the type twice (once in the check and once in the cast) which can lead to typo errors and it seems like unnecessary boilerplate.
So is there any reason why the compiler doesn't support me writting
def fun[T: TypeTag](v: List[T]) = {
v match {
case v3: List[Option[Int] => {
v3.map(x: Int => ...)
}
...
}
}
and then fills in the check and the cast for me?
It seems straight forward to me, but, as I said, I'm probably overlooking something.
Thanks in advance for any helpful explanation.