Type equality in the Scala 2.10 Reflection API

二次信任 提交于 2019-12-22 05:53:46

问题


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

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?


回答1:


That's a bug. Right this morning I was going to investigate and fix it.

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 ==.




回答2:


Another illustration of the strangeness:

scala> val atype = typeOf[A[String]]
atype: reflect.runtime.universe.Type = A[String]

scala> val atype2 = typeOf[F].parents(1)
atype2: reflect.runtime.universe.Type = A[String]

scala> typeOf[F].parents contains atype
res39: Boolean = false

scala> typeOf[F].parents contains atype2
res40: Boolean = true

I think you're seeing a bug similar to this one: https://issues.scala-lang.org/browse/SI-5959 (although I've confirmed that this oddness occurs outside of the REPL too).



来源:https://stackoverflow.com/questions/11254893/type-equality-in-the-scala-2-10-reflection-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!