有时候,当我检查一个类是否扩展了特征时,我会得到不一致的结果。我正在使用Scala 2.12.8。
我的项目使用ru.Type
动态构建操作链。一项操作的输出必须与下一项的输入一致。
在链接两个操作之前,我使用以下命令检查类型是否兼容:
def checkFrom(t: ru.Type): Boolean = t <:< ru.typeOf[ExpectedTrait[_, _, _]]
但是有时候(实际上很少),<:<
返回false而不是true(我没有找到原因)。
我添加了调试消息:
def checkFrom(t: ru.Type): Boolean = {
val expectedType = ru.typeOf[ExpectedTrait[_, _, _]]
val r = t <:< expectedType
if (!r) println(s"$t is not a $expectedType (${t <:< expectedType})")
r
}
我可以看到类似MyClass is not a ExpectedTrait (true)
的消息。
<:<
的第一个评估返回false,第二个评估返回true!
t是Types$ClassNoArtgsTypeRef
,expectedType是Types$ExistentialType
-编辑-
所有ru.Type来自ru.typeOf[X]
。在这种情况下,我不使用rm.classSymbol(x.getClass).toType
。
此问题可能与inconsistency-with-scala-reflection-library
有关是否存在解决方法?