相关疑难解决方法(0)

为什么Scala的不可变Set在其类型中不协变?

编辑:根据原始答案重写这个问题

scala.collection.immutable.Set班是不是在它的类型参数不变性.为什么是这样?

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
}
Run Code Online (Sandbox Code Playgroud)

scala set covariance scala-collections

89
推荐指数
3
解决办法
7386
查看次数

标签 统计

covariance ×1

scala ×1

scala-collections ×1

set ×1