I have a type Foo, with subtypes.
I have another class that contains, among other things, a Foo:
class FooResult(val foo: Foo ...
I have a Set of FooResult, and I wish to pull the Foos out of it with a map(), and then calc the diff between that Set (of Foos) and another Set of Foos. 'results' below is a Set[FooResult] and - this is the critical part - bundle is a Set[_ <: Foo]
val completedFoos = results.map(result => result.calc)
val unfinishedFoos = bundle.foos.diff(completedCalcs)
The second line will not compile. It worked fine when bundle was a Set[Foo] instead of Set[_ <: Foo] - introducing the covariance screws things up. This is the error:
type mismatch;
found : Set[Foo]
required: scala.collection.GenSet[_$1]
Note: Foo >: _$1, but trait GenSet is invariant in type A.
You may wish to investigate a wildcard type such as `_ >: _$1`. (SLS 3.2.10)
I have not been able to find any simple way around this. Forgive my ignorance, but why would these 'helper' types like GenSet be declared invariant?
Am I missing something (very likely) or is this somehow a weakness of Scala's marvelous collections framework (very unlikely, I think)?