我试图用简单的例子来解决我的sbt测试
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
第二个文件可以从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
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会这样.如果编译器发现我的类型为布尔值?
这是错误的:
assert(sum(List() === 0))
Run Code Online (Sandbox Code Playgroud)
因为sum参数的类型是boolean(List() === 0)
尝试:
assert(sum(List()) === 0)改为.
最大功能也是如此.