开发者

+= appends to stack in Scala 2.7.7; :+ does not seem to work in Scala 2.8.0

Using Scala 2.7.7, this works as ex开发者_高级运维pected:

import scala.collection.mutable.Stack
...
var x = new Stack[String]
x += "Hello"
println(x.top)

After changing to Scala 2.8.0, the += should be replaced by :+. However, this does not append to the stack: java.util.NoSuchElementException: head of empty list.

Am I overlooking something basic?


:+, defined in SeqLike, copies the stack and append the element into the new stack, and return that. So x is not modified.

Probably you want .push() instead (example).

var x = new Stack[String]
x.push("Hello")
println(x.top)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜