更新时间:2023-11-24 16:21:46
它似乎适用于 WeakTypeTag
而不是 TypeTag
(也改变 typeTag
到 weakTypeTag
).我真的不知道为什么;找不到任何关于此的具体文档.
Ok so the real reason I ran into this is that my ScalaTests failed to compile, because I defined some of the classes inside of the test scope that call another class file expecting to work with TypeTags. Notice that because class B is within my "test" (pretend this is a scala test call) , typetag no longer becomes viable. I suspect maybe I shouldn't be attempting this on an anonymous class inside of a local scope, but could someone help me understand please? Thanks
import scala.reflect.runtime.universe._
import scala.Symbol
class TypeTagger[T:TypeTag] {
val tt = typeTag[T]
}
object TypeTagger {
def apply[T]()(implicit tt:TypeTag[T]) = new TypeTagger[T]
}
object TestRunTypeTagger extends App {
class A
val test = new TypeTagger[A]
{
class B
val test2 = TypeTagger[B]() //fails
}
}
Error: No TypeTag available for B val test2 = TypeTaggerB
^ not enough arguments for method apply: (implicit tt:
reflect.runtime.universe.TypeTag[B])chorle.scala.testarea.TypeTagger[B] in object TypeTagger. Unspecified value parameter tt. val test2 = TypeTaggerB
^
It seems to work with a WeakTypeTag
instead of a TypeTag
(also change typeTag
to weakTypeTag
). I have no idea why really; couldn't find any documentation about this specifically.