我试图解决我的SBT测试用简单的例子Scala的类型不匹配;发现:需要布尔:列表[INT]
package example
object Lists {
def sum(xs: List[Int]): Int = {
xs match {
case x :: tail => x + sum(tail)
case Nil => 0
}
}
def max(xs: List[Int]): Int = {
xs match {
case Nil => throw new java.util.NoSuchElementException()
case List(x) => x
case x :: y :: rest => max ((if (x > y) x else y) :: rest)
}
}
}
第二个文件可以从 http://www.filedropper.com/listssuite_1
当我尝试下载与SBT测试
[error] /home/milenko/dom1/example/src/test/scala/example/ListsSuite.scala:122: type mismatch;
[error] found : Boolean
[error] required: List[Int]
[error] assert(sum(List() === 0))
[error] ^
[error] /home/milenko/dom1/example/src/test/scala/example/ListsSuite.scala:134: type mismatch;
[error] found : Boolean
[error] required: List[Int]
[error] assert(max(List(1, -4, 2, 6) === 6))
[error] ^
[error] two errors found
[error] (test:compileIncremental) Compilation failed
[error] Total time: 1 s, completed 17/03/2017 10:43:00
我不明白为什么这个happens.How它配备的是编译器发现我喜欢的类型为布尔?