Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6070219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:54:31+00:00 2026-05-23T09:54:31+00:00

I’m reading the Programming in Scala book by the Scala Creator and I’m a

  • 0

I’m reading the Programming in Scala book by the Scala Creator and I’m a bit confuse on the example of Set.

Here it is for Immutable Set:

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

What’s the point of this?

The set is immutable but the variable jetSet is mutable.
1) So every time I add to the set with += it creates a new set? So the variable point to a new set in memory?

2) Shouldn’t it be:
val jetSet = set("cow","sheep","duck") ? Why does it have to be a var? Is there a reason to use var for a immutable set?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:54:31+00:00Added an answer on May 23, 2026 at 9:54 am

    The advantage of immutable data structures, in this case Set, is that they are persistent. For example:

    var jetSet1 = Set("Boeing", "Airbus")
    val jetSet2 = jetSet1 // ... imagine jetSet2 is somewhere else in the program
    jetSet1 += "Lear"
    assert(!jetSet2.contains("Lear"))
    

    Immutability of these Set objects makes it easier to reason about the program because updates to the jetSet1 variable don’t have side effects in other parts of the code (in this case, wherever jetSet2 is used). Although it’s not clear from this example, there are occasions when it’s convenient to store immutable values in mutable var references; most often, the var will have a limited scope (e.g., local to a function).

    Immutable data structures often have clever implementations that are quite efficient. Unfortunately, the Scala collection API is not well documented regarding performance, but I would expect most operations to be roughly O(log N) time. For example, given a large immutable Set s, one should be able to efficiently construct s + x, a new Set with an extra element. Of course, immutability guarantees that s is also preserved. Under the hood, s and s+x will be stored using some kind of tree data-structures with shared components.

    The title of your question suggest you are also looking for advice about using val or var. The rule of thumb is to use val whenever you can conveniently. If a var is necessary, then try to limit the variable’s scope as much as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.