views:

641

answers:

2

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
}
+2  A: 

EDIT: for anyone wondering why this answer seems slightly off-topic, this is because I (the questioner) have modified the question.

Scala's type inference is good enough to figure out that you want CharSequences and not Strings in some situations. In particular, the following works for me in 2.7.3:

import scala.collections.immutable._
def findCharSequences(): Set[CharSequence] = Set("Hello", "World")

As to how to create immutable.HashSets directly: don't. As an implementation optimization, immutable.HashSets of less than 5 elements are not actually instances of immutable.HashSet. They are either EmptySet, Set1, Set2, Set3, or Set4. These classes subclass immutable.Set, but not immutable.HashSet.

Jorge Ortiz
You are right; in trying to simplify my actual example I made a trivial mistake :-(
oxbow_lakes
+8  A: 

Set is invariant in its type parameter because of the concept behind sets as functions. The following signatures should clarify things slightly:

trait Set[A] extends (A=>Boolean) {
  def apply(e: A): Boolean
}

If Set were covariant in A, the apply method would be unable to take a parameter of type A due to the contravariance of functions. Set could potentially be contravariant in A, but this too causes issues when you want to do things like this:

def elements: Iterable[A]

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.

Daniel Spiewak
I guess this argument hinges around "the concept behind sets as functions" - could this be expanded upon? For example, what advantages does "a set as a function" give me that a "set as a collection" not? Is it worth losing the use of that covariant type?
oxbow_lakes
In light of your answer, would you (in general) *not* use Set as the type returned by an API? I would commonly have a service implementation keep an internal Set<XImpl> in Java, with the service API itself wrap this in a Collections.<X>unmodifiable on an access method.
oxbow_lakes
I think I will ask a related question
oxbow_lakes
Sets are really boolean functions by definition; it's a math thing that has nothing to do with Scala. In fact, iterable sets (set as a collection) are an extension to the idea rather than part of the core.
Daniel Spiewak
The type signature is a rather weak example. A set's "apply" is the same thing as it's contains method. Alas, Scala's List is co-variant and has a contains method as well. The signature for List's contains is different, of course, but the method works just like Set's. So there is nothing really stopping Set from being co-variant, except a design decision.
Daniel
Why not have Set extend (AnyRef=>Boolean) instead of (A=>Boolean)?
Craig P. Motlin
Sets are not boolean functions from a mathematical perspective. Sets are "built up" from the Zermelo-Fraenkel axioms not *reduced* by some inclusion function. The reason behind this is Russell's paradox: if anything can be a member of a set, then consider the Set R of sets which are not members of themselves. Then ask the question, is R a member of R?
oxbow_lakes