Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

immutability - Programming in Scala example confusion (Listing 3.5, creating mutable and immutable sets)

I'm having some confusion understanding immutable and mutable sets from Listing 3.5 in the Programming in Scala, 3rd edition book. The paragraph is telling me that it is creating immutable sets and, then updating it with +=, but then the new stored set contains all three elements. Some questions:

  • How is this different from having a mutable set? Not sure I'm seeing the distinction.
  • What happens to the old set that is existed before reassignment?
  • Why bother calling it an immutable set if you can just reassign a new set to the jetSet variable?
  • Is someone trolling me and altered "mutable" and "immutable" in the PDF? (Don't worry, I've also bought the actual book which will arrive soon.)

From the book:

The default way to create a set is shown in Listing 3.5:

var jetSet = Set("Boeing", "Airbus") 
jetSet += "Lear" 
println(jetSet.contains("Cessna"))

Listing 3.5 - Creating, initializing, and using an immutable set. Figure 3.2 - Class hierarchy for Scala sets (not included). In the first line of code in Listing 3.5, you define a new var named jetSet and initialize it with an immutable set containing the two strings, "Boeing" and "Airbus". As this example shows, you can create sets in Scala similarly to how you create lists and arrays: by invoking a factory method named apply on a Set companion object. In Listing 3.5, you invoke apply on the companion object for scala.collection.immutable.Set, which returns an instance of a default, immutable Set. The Scala compiler infers jetSet's type to be the immutable Set[String].

To add a new element to a set, you call + on the set, passing in the new element. On both mutable and immutable sets, the + method will create and return a new set with the element added. In Listing 3.5, you're working with an immutable set. Although mutable sets offer an actual += method, immutable sets do not.

In this case, the second line of code, "jetSet += "Lear"", is essentially a shorthand for:

jetSet = jetSet + "Lear"

Thus, in the second line of Listing 3.5, you reassign the jetSet var with a new set containing"Boeing", "Airbus", and "Lear". Finally, the last line of Listing 3.5 prints out whether or not the set contains the string "Cessna". (As you'd expect, it prints false.)

If you want a mutable set, you'll need to use an import, as shown in Listing 3.6:

import scala.collection.mutable

val movieSet = mutable.Set("Hitch", "Poltergeist") 
movieSet += "Shrek"
println(movieSet)

Thanks in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

jetSet is a var. It references a new and different Set after the += assignment.

What happens to the old set that is existed before reassignment?

If the previous 2-element Set is not referenced by any other variable then it is garbage collected away.

movieSet is a val. It will always reference the same Set even if that Set mutates its value.

Mutation in general is considered undesirable and experienced Scala coders will avoid both of these scenarios.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...