几天前,我发现了Paul Philip的代码片段https://gist.github.com/paulp/9085746,展示了一种非常奇怪的行为。我没有找到任何解释这种行为的方法。
简化的代码片段:
那里在发生什么?
简化的代码片段:
val buf = new ListBuffer[Int]()
buf ++= Seq(1,2,3)
val lst: List[Int] = buf.toIterable.toList
println(lst) //List(1,2,3)
buf ++= Seq(4,5,6)
println(lst) //List(1,2,3,4,5,6)
没有使用 toIterable
时,它按预期工作。
val buf = new ListBuffer[Int]()
buf ++= Seq(1,2,3)
val lst: List[Int] = buf.toList
println(lst) //List(1,2,3)
buf ++= Seq(4,5,6)
println(lst) //List(1,2,3)
那里在发生什么?
buf -= 3
也会改变lst
。 - Dima