Intersect Scala set with set of subtype
generics, scala
Solution
A `Set` is not covariant on its type parameter.
So a simple solution is to convert to `List` (which is covariant):
def intersection(s1: Set[MyType], s2: Set[_ <: MyType]) =
s1.toList.intersect(s2.toList).toSet
Problem
Why doesn't this function compile? ``` case class MyType(n: Int) def intersection(s1: Set[MyType], s2: Set[_ <: MyType]) = (s1 & s2) ``` I get the following error: error: type mismatch; found : Set[_$1] where type _$1 <: MyType required: scala.collection.GenSet[MyType] Note: _$1 <: MyType, but trait GenSet is invariant in type A. You may wish to investigate a wildcard type such as `_ <: MyType`. (SLS 3.2.10) (w & r) Is there a simple way to "promote" the second argument to type Set[MyType] without using asInstanceOf?