As also seen in the scaladoc, sets are invariant :( So what do you do when you wish treat one as if it were covariant?
I have a constructor object:
object SimpleGraph {
def apply[ID](vertices: List[AbstractVertex[ID]], edges: Set[AbstractEdge[ID]]) = {
val simpleGraph = new SimpleGraph[ID, AbstractVertex[ID], AbstractEdge[ID]]
vertices.map(simpleGraph.addNode)
edges.map(simpleGraph.addEdge)
simpleGraph
}
}
Given the invariance, sets of types that adhere to T <: AbstractEdge[ID]
won't make it through into my apply
function. The compilation error suggests using a wildcard type, which as much as I fathom means circumventing type safety altogether (?). My own solution is ugly because it uses cpu cycles and more memory to get over the invariance: changing the prototype's type from Set
to List
and converting back and forth between a list and a set to get the data through.
Have you got a better suggestion?
Thanks!