我有一個API(來自第三方Java庫),看起來像:遍歷Java列表,涉及Java泛型斯卡拉
public List<?> getByXPath(String xpathExpr)
defined on a class called DomNode
我試試這個斯卡拉:
node.getByXPath(xpath).toList.foreach {node: DomElement =>
node.insertBefore(otherNode)
}
但node.getByXPath上出現編譯錯誤。 錯誤: 「類型不匹配;實測值:(com.html.DomElement)=>所需單位:=>其中類型0(0?)?」
如果我改變成:
node.getByXPath(xpath).toList.foreach {node =>
node.insertBefore(otherNode)
}
然後錯誤消失,但然後我得到錯誤node.insertBefore(otherNode) 錯誤:「值insertBefore不是?0的成員」
這個問題的答案是什麼?
這是一個很好的觀點,至少在部分功能方面,如果它不是DomElement,您有機會做某些事情。但是,如果你不提供另一個'case',它基本上與演員相當 - 如果它不是預期的類型,你會得到一個異常。 – sourcedelica 2011-06-16 04:55:10