我有一个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