Scala 模式匹配 :+ +: 运算符

use*_*752 3 scala pattern-matching

我对 Scala 模式匹配中的 :+ 和 +: 运算符感到困惑。

我有以下函数应该返回列表中的最后一个值

object Solution {

    def last[A](seq: Seq[A]) : A = seq match {
        case head +: Nil => head
        case head +: tail => last(tail)
    }

    def main(args: Array[String]) {
        println("1: " + last(List(1, 2, 3, 4)))
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我在运行代码时收到一些错误

error: not found: value +:
error: not found: value head
error: not found: value +:
error: not found: value tail
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

编辑:我用 Scala 2.9.2 运行它

som*_*ytt 5

提取器是对象,因此您需要查找+:.

在 2.11,

scala> +:
res0: collection.+:.type = scala.collection.$plus$colon$@5f2050f6
Run Code Online (Sandbox Code Playgroud)

但比较

http://www.scala-lang.org/api/2.11.0/#scala.collection.$plus$colon$

http://www.scala-lang.org/api/2.9.2/#scala.collection.package

那里没有这样的提取器。