EDYCJA : Ponownie napisano to pytanie na podstawie oryginalnej odpowiedzi
scala.collection.immutable.SetKlasa nie jest kowariantna w jego parametr typu. Dlaczego to?
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
}
foo(s.toSet[CharSequence])kompiluje się dobrze.toSetSposób O (1) - nie tylko otaczaasInstanceOf.