类型别名搞砸了类型标签? [英] Type aliases screw up type tags?

查看:65
本文介绍了类型别名搞砸了类型标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么类型标签不能与类型别名一起使用.例如.给定

Why don't type tags work with type aliases. E.g. given

trait Foo
object Bar {
  def apply[A](implicit tpe: reflect.runtime.universe.TypeTag[A]): Bar[A] = ???
}
trait Bar[A]

我想在以下方法中使用别名,因为我需要键入 A 大约两打:

I would like to use an alias within the following method, because I need to type A around two dozen times:

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
}

所以我似乎根本不能使用我的别名.

So it seems I can't be using my alias at all.

推荐答案

改用weakTypeOf.反射在内部区分全局可见和局部声明,因此您也需要区别对待它们.这个疣可能会在更高版本的 Scala 中移除.

Use weakTypeOf instead. Reflection internally distinguishes globally visible and local declarations, so you need to treat them differently as well. This wart may be removed in later versions of Scala.

这篇关于类型别名搞砸了类型标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆