14
votes

I understand generally what type erasure is and why we would encounter unchecked warnings. However, I don't understand why only one unchecked warning is issued in the following case:

class A[K] {
  def receive: PartialFunction[Any, Unit] = {
    case ds: List[Double] =>  // unchecked warning
      println("* List[Double]")
    case kx: Vector[K] =>      // no unchecked warning
      println("* Vector[K]")
  }
}

object TestApp extends App {
  val a = new A[Int]
  a.receive(List("bar"))
  a.receive(Vector("foo"))
}

Both receive calls unfortunately match case clauses. Compiler did issue an warning for the first clause:

Warning: non-variable type argument Double in type pattern List[Double] is unchecked since it is eliminated by erasure.

I know TypeTag[T] can be used to achieve better type safety. But my concern here is why no unchecked warning is issued for the second case clause. As far as I think, type argument K is also erased and according to Java Generics FAQ

"unchecked" warnings are also reported when the compiler finds a cast whose target type is either a parameterized type or a type parameter

So I wonder why isn't there an unchecked warning?

1
Looks like a bug to me.Travis Brown
@Travis, you think so? Alright, I'll post this question to scala-user mailing-list to get more input before sending a bug report.cfchou
Yep, that's a bug. Please open a ticket, I'll take a look at the problem.retronym
I've lodged issues.scala-lang.org/browse/SI-9188 to track this.retronym
@retronym, thanks. Could you please delete SI-9189 which I at the same time created?(I don't have delete permission)cfchou

1 Answers

0
votes

It's probably a bug. SI-9188 is open for that.