As I understand it, the point of having a +=
method on mutable Sets, is that
val s = collection.mutable.Set(1)
s += 2 // s is now a mutable Set(1, 2)
has an analogous effect to
var s = Set(1) // immutable
s += 2 // s is now an immutable Set(1, 2)
If that's so, why does the +=
method on the mutable Set return the Set itself? Wouldn't that make code more difficult to refactor e.g.
val s = collection.mutable.Set(1)
val s1 = s += 2 // s and s1 are now mutable Set(1, 2)
can't be refactored to
var s = Set(1) // immutable
var s1 = s += 2 // s is immutable Set(1, 2), s1 is now ()
while maintaining the original meaning. What's the reason behind this design decision?