Push vs += in mutable.Stack

I have a strong (possibly misguided) expectation in Scala collections that += adds an element to a collection in the “default way”. Therefore, I was shocked to discover that in the new ArrayDeque implementation of mutable.Stack, the push method and the += method add to opposite ends of the stack, with push adding to the “top” and += adding to the “bottom”.

I understand exactly how this happened (+= was inherited from ArrayDeque) but it feels incredibly broken. And it has tripped up more than one of my students.

Although it would be trivial to fix, I also realize that any such fix risks breaking existing code. At the minimum, I would hope for a prominent warning in the API.

(This problem didn’t arise in the old version of Stack because it didn’t support +=.)


I would expect

a += b

to behave like

a = a + b

which would be adding to the right. I am confused, however, how left and right translate to first/last in (or top and bottom). So, definitely, the docs ought to be much clearer.

I’m also a bit confused why it is += and not :+=.

A way to add to bottom of a stack sounds like a leaky abstraction

Hey Chris,

I think you made a good point. I agree that the current situation can be confusing. I’m afraid the only thing we can do is to deprecate the += operation in mutable.Stack.


That sounds reasonable.
Not that += could ever be removed as long as Stack visibly extends ArrayDeque, but at least there would be a warning.

conceptually, shouldn’t ArrayDeque extend Stack?