CHỈNH SỬA : Đã viết lại câu hỏi này dựa trên câu trả lời ban đầu
Các scala.collection.immutable.Setlớp học không được hiệp biến trong tham số kiểu của nó. Tại sao thế này?
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])biên dịch tốt. CáctoSetphương pháp là O (1) - nó chỉ kết thúc tốt đẹpasInstanceOf.