当我编写一些Scala代码时,在尝试编译代码时遇到了一个奇怪的错误消息。我把代码分解成了一个更简单的代码(从语义的角度来看,这完全没有意义,但仍然显示错误)。编译for循环时出现奇怪的错误
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.collection.mutable.ListBuffer
val map = scala.collection.mutable.Map[Int, ListBuffer[Int]]()
for (i <- 1 to 2) {
map.get(0) match {
case None => map += (1 -> ListBuffer[Int](1))
case Some(l: ListBuffer[Int]) => l += i
}
}
// Exiting paste mode, now interpreting.
<console>:12: error: type arguments [Any] do not conform to trait Cloneable's t
pe parameter bounds [+A <: AnyRef]
for (i <- 1 to 2) {
^
当for循环的末尾添加一个额外的行,代码工作:
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.collection.mutable.ListBuffer
val map = scala.collection.mutable.Map[Int, ListBuffer[Int]]()
for (i <- 1 to 2) {
map.get(0) match {
case None => map += (1 -> ListBuffer[Int](1))
case Some(l: ListBuffer[Int]) => l += i
}
1 // <- With this line it works
}
// Exiting paste mode, now interpreting.
warning: there were 1 unchecked warnings; re-run with -unchecked for details
import scala.collection.mutable.ListBuffer
map: scala.collection.mutable.Map[Int,scala.collection.mutable.ListBuffer[Int]]
= Map(1 -> ListBuffer(1))
我想,它有事情做与比赛-case语句的返回值。但是我不是Scala专家,无法弄清楚这个错误信息背后的原因以及我做错了什么。我希望有更聪明的人可以帮忙。
错误信息背后的原因是什么?匹配情况声明有什么问题?
更新:使用Scala 2.9.2
这可能是一个错误。随着2.10它工作正常。 – sschaef 2012-08-11 18:41:38