我使用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的「比較無關類型」警告是不可靠的。我在幾個地方看到了這些警告是錯誤的。 –