我使用IntelliJ IDEA的Scala插件,并且发现该插件的奇怪行为。让我告诉你一个代码片段:IntelliJ IDEA:检测不能是相同类型的表达式的比较(==和!=)
def fun: Option[Any] => Int = {
case Some(x) if x.isInstanceOf[Int] => x.asInstanceOf[Int]
case None => 0
}
关于Scala代码,IDEA给我一个警告:
Comparing unrelated types
Detects comparisons (== and !=) of expressions which cannot be the same type
和它突出的下一条语句x.isInstanceOf[Int]
。我只使用isInstanceOf
运算符来确定x
的类型。这是插件的错误还是我错过了Scala语法中的某些东西?
你想,如果个数为int然后返回其他数字0? –
是的,我想通过模式匹配来做到这一点。 – Finkelson
根据我的经验,IntelliJ IDEA的“比较无关类型”警告是不可靠的。我在几个地方看到了这些警告是错误的。 –