为什么类型标记不能与类型别名一起工作。例如,给定
trait Foo
object Bar {
def apply[A](implicit tpe: reflect.runtime.universe.TypeTag[A]): Bar[A] = ???
}
trait Bar[A]我想在下面的方法中使用别名,因为我需要输入大约24次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
}看起来我根本不能用我的化名。
发布于 2013-02-05 18:40:35
请改用weakTypeOf。反射在内部区分全局可见声明和局部声明,因此您也需要区别对待它们。在更高版本的Scala中,这个瘤可能会被移除。
发布于 2013-02-04 07:36:24
更改def apply声明:
import scala.reflect.runtime.universe._
trait Foo
object Bar {
def apply[A]()(implicit tpe: TypeTag[A]): Bar[A] = ???
}
trait Bar[A]
class test {
type A = Foo
implicit val foo = typeOf[A]
def test = Bar[A]()
}https://stackoverflow.com/questions/14678292
复制相似问题