Scala 2.10 反射 API 中的类型相等 [英] Type equality in the Scala 2.10 Reflection API
问题描述
我在 Scala 2.10.0 Milestone 4 中遇到了一个奇怪的反射问题 我无法绕过去.首先是按照我期望的方式工作的东西:
I'm running into a weird issue with reflection in Scala 2.10.0 Milestone 4 that I can't wrap my head around. First for the stuff that works the way I'd expect:
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> trait A[X]; trait B[Y] extends A[Y]
defined trait A
defined trait B
scala> typeOf[B[String]].parents
res0: List[reflect.runtime.universe.Type] = List(java.lang.Object, A[String])
scala> typeOf[B[String]].parents contains typeOf[A[String]]
res1: Boolean = true
同样(在同一会话中):
Similarly (in the same session):
scala> trait D; trait E extends A[D]
defined trait D
defined trait E
scala> typeOf[E].parents
res2: List[reflect.runtime.universe.Type] = List(java.lang.Object, A[D])
scala> typeOf[E].parents contains typeOf[A[D]]
res3: Boolean = true
这里没有什么惊喜:我可以要求一个类型的父母并得到我所期望的.现在我基本上结合了上面的两个例子:
No surprises here: I can ask for a type's parents and get exactly what I expect. Now I essentially combine the two examples above:
scala> trait F extends A[String]
defined trait F
scala> typeOf[F].parents
res4: List[reflect.runtime.universe.Type] = List(java.lang.Object, A[String])
scala> typeOf[F].parents contains typeOf[A[String]]
res5: Boolean = false
我不明白这怎么可能是假的.如果我有 F
扩展 A[Seq[D]]
、A[Int]
等,也会发生同样的事情.我的概括是什么?我错过了这会使这种行为有意义吗?
I don't understand how this could be false. The same thing happens if I have F
extend A[Seq[D]]
, A[Int]
, etc. What's the generalization I'm missing that would make this behavior make sense?
推荐答案
这是一个错误.就在今天早上,我正要调查并修复它.
That's a bug. Right this morning I was going to investigate and fix it.
编辑.这似乎是泄漏到用户空间的 Scala 反射 API 的实现细节.修复起来并不容易,所以现在我们保持原样,但会研究改进的可能性.
Edit. this appears to be an implementation detail of Scala reflection API leaking to the userland. It's not easy to fix, so for now we leave it as it is, but will look into the possibilities to improve.
同时,为了得到正确的结果,应该总是使用=:=
来比较类型,而不是==
.
In the meanwhile, to get correct results, one should always use =:=
to compare types, not ==
.
这篇关于Scala 2.10 反射 API 中的类型相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!