views:

32

answers:

1

Using Scala 2.7.7, this works as expected:

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?

+1  A: 

:+, 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)
KennyTM
Thanks! That solves it.
Villadsen