为什么不能使用类型别名来处理类型标签。例如。鉴于类型别名拧紧类型标签?
trait Foo
object Bar {
def apply[A](implicit tpe: reflect.runtime.universe.TypeTag[A]): Bar[A] = ???
}
trait Bar[A]
我想下面的方法中使用别名,因为我需要键入A
围绕两个十几次:
def test {
type A = Foo
implicit val fooTpe = reflect.runtime.universe.typeOf[A] // no funciona
Bar[A] // no funciona
}
下一个尝试:
def test {
type A = Foo
implicit val fooTpe = reflect.runtime.universe.typeOf[Foo] // ok
Bar[A] // no funciona
}
所以似乎我根本无法使用我的别名。
好的。但是,我是否也需要更改声明网站?因为我似乎没有从由'weakTypeOf'返回到'TypeTag [A]'的类型进行隐式转换。例如。 'found:reflect.runtime.universe.Type; required:reflect.runtime.universe.TypeTag [A]'(当调用Bar [A](fooTpe)') –
好吧,好像我在从手机回答时忽略了一些东西。首先,'typeOf'和'weakTypeOf'的结果是一个'Type',而不是'TypeTag',所以'fooTpe'不适合'Bar.apply'。 –
其次,您可以完全摆脱'weakTypeOf'并将'TypeTag [A]'改为'WeakTypeTag [A]'。之后,它会正常工作。 –