my code is as below.
case class C[T]() {
val pf:PartialFunction[Any,Any] = {
case i:T => i
}
}
println(C[Int]().pf.isDefinedAt(-1.0))
this prints true. Why this happens?
my code is as below.
case class C[T]() {
val pf:PartialFunction[Any,Any] = {
case i:T => i
}
}
println(C[Int]().pf.isDefinedAt(-1.0))
this prints true. Why this happens?
Copyright © 2021 Jogjafile Inc.
Due to type erasure your code is basically equal to:
you can use
TypeTags to fix it:in use:
these are virtually equal to
where the type
Uis inferred - it has a limitation that it can only be as precise as compiler's knowledge about the type whenTypeTagis required.You can also try to use
ClassTag[T]to use runtime reflection... but it would fail for primitiveswhich results in
Thing is
classTagwould resolve to Scala'sintwhile runtime would showjava.lang.Int:In general there is no perfect solution here, you can read more about similar questions here and here.