《Scala编程》第三版Listing3.5中可变与不可变Set的疑问
Hey there! Let's break down your confusion step by step—this is a super common point of confusion when first learning Scala's mutable vs immutable collections, so you're not alone!
First, let's clarify the fundamental distinction between these two types of sets, which will answer most of your questions:
- Immutable Set: Once created, the collection itself can never be modified. The
jetSet += "Lear"you see isn't adding an element to the original set—it's syntactic sugar forjetSet = jetSet + "Lear". The+method on an immutable set creates a brand-new immutable set with the new element, and we just reassign this new set to thevarvariablejetSet. The original set (containing "Boeing" and "Airbus") remains completely unchanged. - Mutable Set: The collection itself can be modified in-place. When you call
movieSet += "Shrek", you're directly adding the element to the existing set object—no new set is created, and the variablemovieSetkeeps pointing to the same collection instance the whole time.
Your Specific Questions Answered
1. What happens to the old immutable set before reassignment?
The original immutable set (with "Boeing" and "Airbus") will be handled by Scala's garbage collector (GC). As long as no other variables or code hold a reference to it, the GC will free up its memory when it's no longer needed. Since immutable sets can't be modified, even if other parts of your code did reference the old set, you'd never have to worry about its contents changing unexpectedly—this is one of the key benefits of immutable collections (thread safety, no side effects).
2. If we can reassign the variable, why is it called an immutable set?
This is all about separating variable mutability (var/val) from collection mutability (immutable/mutable):
var jetSet: This means the variablejetSetcan be reassigned to point to different objects.immutable.Set: This means the collection object itself has fixed contents—you can't add, remove, or change elements in it after creation.
Even though you're reassigning thevarto a new immutable set, each individual set instance remains unchangeable. If you changedjetSetto aval, you wouldn't be able to use+=at all—becausevalvariables can't be reassigned, and immutable sets don't have an in-place+=method.
3. Did someone mix up "mutable" and "immutable" in the PDF?
Nope, the PDF content is correct! The behavior you're seeing is exactly how Scala's immutable collections are designed:
- Immutable sets use
+to return new collections, and+=is just syntactic sugar for reassignment (only works withvarvariables). - Mutable sets have a true in-place
+=method that modifies the original collection.
When your physical book arrives, you'll find the same example—it's a classic way to teach the difference between variable mutability and collection mutability in Scala.
A Quick Side-by-Side Example to Drive the Point Home
Immutable Set (with var variable)
var jetSet = scala.collection.immutable.Set("Boeing", "Airbus") val originalSet = jetSet // Save a reference to the original set jetSet += "Lear" println(jetSet) // Output: Set(Boeing, Airbus, Lear) println(originalSet) // Output: Set(Boeing, Airbus) — original set is untouched!
Mutable Set
import scala.collection.mutable val movieSet = mutable.Set("Hitch", "Poltergeist") val originalSet = movieSet // Save a reference to the original set movieSet += "Shrek" println(movieSet) // Output: Set(Shrek, Hitch, Poltergeist) println(originalSet) // Output: Set(Shrek, Hitch, Poltergeist) — original set was modified!
内容的提问来源于stack exchange,提问作者newbie

