I have an API (from third party java library) that looks like:
public List<?> getByXPath(String xpathExpr)
defined on a class called DomNode
I try this in scala:
node.getByXPath(xpath).toList.foreach {node: DomElement =>
node.insertBefore(otherNode)
}
But I get compile error on node.getByXPath.
error: “type mismatch; found : (com.html.DomElement) => Unit required: (?0) => ? where type ?0”
If I change it into:
node.getByXPath(xpath).toList.foreach {node =>
node.insertBefore(otherNode)
}
then the error goes away but then I get error on node.insertBefore(otherNode)
error: “value insertBefore is not a member of ?0”
What is the answer to this problem?
You’ll have to cast it. ie,
You would have the same issue in Java as the type of List elements is unknown.
(I am assuming every element actually is a DomElement)
EDIT:
Daniel is right, there is a better way to do this. For example, you can throw a much nicer exception (as compared to ClassCastException or MatchError). Eg.