這裏是我的代碼從列表斯卡拉測試意外的錯誤,「)」預期,但「}」發現
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