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 9264391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:48:57+00:00 2026-06-18T13:48:57+00:00

Consider the following: scala> val myset = Set(1,2) myset: scala.collection.immutable.Set[Int] = Set(1, 2) scala>

  • 0

Consider the following:

scala> val myset = Set(1,2)
myset: scala.collection.immutable.Set[Int] = Set(1, 2)

scala> myset += 3
<console>:9: error: reassignment to val
              myset += 3
                    ^

scala> var myset = Set(1,2)
myset: scala.collection.immutable.Set[Int] = Set(1, 2)

scala> myset += 3

scala> myset
res47: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

In one case I can add “alex” and in another I can’t.
I know the difference between val and var. However what confused me in both cases Scala tells me that the Set is immutable but it allows different behaviour.

I don’t want to just as that’s because in oncase myset is a val and in one it is a var. I want a deeper answer than that to explain why in both cases Scala says myset is an immutable set but yet treats both differently. Because it is counter intuitive.

For example, is there any difference using a mutuable set and declaring an immutable set as var?
And why does scala let you bend the rules? Would it not be better if when it is said immutable it meant it?

Thanks.

  • 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-06-18T13:48:58+00:00Added an answer on June 18, 2026 at 1:48 pm

    First of all let’s translate the += call

    val myset = Set(1,2)
    myset = myset + 3 //this is what the compiler does for myset += 3
    

    This means that we’re actually calling the + method on Set, whose scaladoc says

    Creates a new set with an additional element, unless the element is already present.

    What the code is trying to do is therefore to create a new Set with the added element and reassign it to the immutable reference myset.

    Now if we change the reference to a mutable one (using var) then you can reassign it with the newly made and immutable Set(1,2,3)

    The original Set(1, 2) is still immutable and no rule is broken. Let’s explain this with a case

    var myset = Set(1,2)
    val holdIt = Some(myset)
    myset += 3
    println(myset) // will print Set(1, 2, 3)
    println(holdIt)// will print Some(Set(1, 2))
    

    As you can see, the original Set, which is captured by the Option, was never changed, we just reassigned it’s variable reference to a newly created Set

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

Sidebar

Related Questions

Consider the following definition in Scala: val f = ((_: Int) + 1).toString() The
Consider the following: val stuff = Map[String, Int](apple -> 5, orange -> 1, banana
Using Scala 2.9.1, consider the following two instances of Either : scala> val er:
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>
Consider the following: scala> val a:java.lang.Boolean = true a: java.lang.Boolean = true scala> val
Consider the following Scala case class: case class WideLoad(a: String, b: Int, c: Float,
First. Consider the following code scala> val fail = (x: Any) => { throw
Consider following 2 programs giving same error First calss: public class Testing { Testing
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider following script: SET LANGUAGE 'German' GO SET DATEFIRST 1 GO DECLARE @FullDate DATETIME

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.