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
}
Best Answer
Set
is invariant in its type parameter because of the concept behind sets as functions. The following signatures should clarify things slightly:If
Set
were covariant inA
, theapply
method would be unable to take a parameter of typeA
due to the contravariance of functions.Set
could 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.Map
is also invariant in one of its type parameters.