这里是我的代码从列表斯卡拉测试意外的错误,“)”预期,但“}”发现
object kp {
def main(args: Array[String]) {
def max(xs: List[Int]): Int = xs match {
case Nil => throw new java.util.NoSuchElementException()
case List(x: Int) => x
case x :: y :: rest => max((if (x > y) x else y) :: rest)
}
val a = 1 :: 4 :: 5 :: -4:: Nil
println(max(a))
}
}
当我想测试我的示例文件夹中的代码与SBT找到最大
[info] Compiling 1 Scala source to /home/milenko/example/target/scala-2.11/classes...
[error] /home/milenko/example/src/main/scala/example/ListsSuite.scala:138: ')' expected but '}' found.
[error] }
[error] ^
[error] /home/milenko/example/src/main/scala/example/ListsSuite.scala:148: ')' expected but '}' found.
[error] }
[error] ^
[error] two errors found
错误指
test("maximum with one negative number") {
assert(max(List(1,4,5,-4)) === 5)
}
和
test("maximum with some repeated elements"){
assert(max(List(2,2,2,2)) === 2)
}
我没有线索,为什么这happens.Here是整个文件
http://www.filedropper.com/listssuite
现在我已经删除了该文件的一些测试,它只有135 lines.But我得到了相同的
wc -l ListsSuite.scala
135 ListsSuite.scala
[email protected]:~/example/src/test/scala/example$ cd ~/example
[email protected]:~/example$ sbt
[info] Loading global plugins from /home/milenko/.sbt/0.13/plugins
[info] Loading project definition from /home/milenko/example/project
[info] Set current project to progfun1-example (in build file:/home/milenko/example/)
> test
[info] Compiling 1 Scala source to /home/milenko/example/target/scala-2.11/classes...
[error] /home/milenko/example/src/main/scala/example/ListsSuite.scala:138: ')' expected but '}' found.
[error] }
[error] ^
[error] /home/milenko/example/src/main/scala/example/ListsSuite.scala:148: ')' expected but '}' found.
[error] }
[error] ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed 16/03/2017 13:43:43
非常奇怪,做什么:138和:148表示什么?
看起来像ListsSuite.scala中的语法错误,可能在报告的位置之前。你可以添加完整的文件吗? – Harald
请提供更多详情。如果你想,我有一个小例子来检查你的函数'max'。并没有什么建议,你不需要引发异常,你的功能不纯粹是功能性的,因为有副作用。 – alifirat
它编译和运行我sbt – FatTail