问题描述
好的,所以我遇到这个问题的真正原因是我的 ScalaTests 无法编译,因为我在测试范围内定义了一些类,这些类调用另一个期望与 TypeTags 一起使用的类文件.请注意,因为 B 类在我的测试"中(假设这是一个 Scala 测试调用),因此 typetag 不再可行.我怀疑也许我不应该在本地范围内的匿名类上尝试这个,但是有人可以帮我理解吗?谢谢
import scala.reflect.runtime.universe._导入 scala.Symbol类 TypeTagger[T:TypeTag] {val tt = typeTag[T]}对象类型标签{def apply[T]()(implicit tt:TypeTag[T]) = new TypeTagger[T]}对象 TestRunTypeTagger 扩展应用程序 {A级val 测试 = 新的 TypeTagger[A]{B级val test2 = TypeTagger[B]()//失败}}
错误:没有可用于 B 的 TypeTagval test2 = TypeTaggerB
^ 方法的参数不足:(隐式 tt:
reflect.runtime.universe.TypeTag[B])chorle.scala.testarea.TypeTagger[B]在对象 TypeTagger 中.未指定值参数 tt.val test2 = TypeTaggerB
^
它似乎适用于 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
^
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.
这篇关于类型标签在代码块范围内不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!