EDIT: Re-written this question based on original answer
The scala.collection.immutable.Set class is not covariant in its type parameter. Why is this?
import scala.collection.immutable._ def foo(s: Set[CharSequence]): Unit = { println(s) } def bar(): Unit = { val s: Set[String] = Set('Hello', 'World'); foo(s); //DOES NOT COMPILE, regardless of whether type is declared //explicitly in the val s declaration }
Setis invariant in its type parameter because of the concept behind sets as functions. The following signatures should clarify things slightly:If
Setwere covariant inA, theapplymethod would be unable to take a parameter of typeAdue to the contravariance of functions.Setcould potentially be contravariant inA, but this too causes issues when you want to do things like this:In short, the best solution is to keep things invariant, even for the immutable data structure. You’ll notice that
immutable.Mapis also invariant in one of its type parameters.