首页 > 解决方案 > Scala协方差推断类型List [Any]?

问题描述

我遇到了以下 Scala 片段,它表示List带有“cons”运算符的 a(类似于::):

sealed trait List[+A] {
    def ::[A](that: A) = Cons(that, this)
}

final case object Nil extends List[Nothing]
final case class Cons[+A](head: A, tail: List[A]) extends List[A]

为什么List[Any]推断的返回类型是::

标签: scalacovariance

解决方案


推荐阅读