Due to type erasure,
wtf.isInstanceOf[Option[String]]
can only check thatwtf
is an instance ofOption
, but not the type parameter. Similarly,asInstanceOf[T]
is actually a cast toObject
at the runtime, and so it succeeds. You need to doclassTag[T].runtimeClass.cast(x)
instead.
The compiler can't use the information from asserts passing (you can imagine a compiler which could, but Scala simply isn't designed like that). It only knows that the type of
wtf
isOption[Int]
, so of course you can't initialize anOption[String]
with it. If you want to get something like that, you needwtf match { case wtf2: Option[String] => ... }
Of course, this doesn't work correctly due to point 1.
Scala type inference fail?
-
13-06-2023 - |
Pergunta
How is this possible:
import scala.util.{Try, Success}
import reflect._
case class Foo[A](x: A) extends Dynamic {
def get[T: ClassTag]: Option[T] = Try(x.asInstanceOf[T]) match {
case Success(r) => Some(r)
case _ => None
}
}
object Foo extends App {
val test = Foo("hi")
val wtf: Option[Int] = test.get[Int]
assert(wtf.isInstanceOf[Option[String]])
assert(wtf == Some("hi")) // how????
// val wtf2: Option[String] = wtf // does not compile even if above assert passes!!
}
Inspired by this question: Scala check type of generics
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow