1

I am from Java background and trying to learn Scala.

There is an example as below in the book "Programming in Scala - Third Edition"

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

The first line creates an immutable non-final Set:

jetSet: scala.collection.immutable.Set[String] = Set(Boeing, Airbus)

The second line runs with no errors.

Now, when I change the first line from var to val, the second line gives error:

<console>:13: error: value += is not a member of scala.collection.immutable.Set[String]
   jetSet += "Lear"

I am not able to understand the reason behind it. val corresponds to final and var is non-final. I am not assigning the variable to any new Set. In both the cases, the Set is immutable.

How is var or val impacting things here?

Also, when jetSet is immutable in both the cases, why is it allowing to add a new value to the set in the case of var. Does it not violate the immutability principle.

Correct my understanding, please.

vijayinani
  • 2,548
  • 2
  • 26
  • 48
  • 1
    Of course `var` violate immutability, better to pretend it doesn't exist – cchantep Oct 11 '17 at 10:45
  • 1
    Possible duplicate of [What is the difference between a var and val definition in Scala?](https://stackoverflow.com/questions/1791408/what-is-the-difference-between-a-var-and-val-definition-in-scala) – Raúl Reguillo Carmona Oct 11 '17 at 10:45
  • It is not violating the immutability principle, as before adding 'Lear', I assigned the jetSet to a new temp var. After adding 'Lear', temp and jetSet were pointing to two different sets. – vijayinani Oct 11 '17 at 10:54
  • I think as the Set is immutable and when I try to add a new value, it tries to create a copy (new Set) and add the value and assign to val. As val is final, it does not allow. Hence += does not allow. – vijayinani Oct 11 '17 at 10:56
  • If I use a mutable Set, then val will also work. When I append a new value to mutable Set, the same Set will be changed (new Set will not be created). Hence, += will work. – vijayinani Oct 11 '17 at 11:02

1 Answers1

1

Case: 1

scala> var jetSet = Set("Boeing", "Airbus")
jetSet: scala.collection.immutable.Set[String] = Set(Boeing, Airbus)

scala> jetSet
res3: scala.collection.immutable.Set[String] = Set(Boeing, Airbus)

scala> jetSet += "Lear"

scala> jetSet
res5: scala.collection.immutable.Set[String] = Set(Boeing, Airbus, Lear)

Case: 2

scala> val jetSet = Set("Boeing", "Airbus")
jetSet: scala.collection.immutable.Set[String] = Set(Boeing, Airbus)

scala> jetSet += "Lear"
<console>:13: error: value += is not a member of scala.collection.immutable.Set[String]
       jetSet += "Lear"
              ^

In case: 1 when var is used. += is understood as follows

jetSet += "Lear" is nothing but jetSet = jetSet + "Lear"

+ will create a new Set with "Lear" added to it along with others already in jetSet Set.

Note that here only reference is mutable but set is immutable. + method creates a new set (new reference code will be assigned to mutable variable.)

case 2: When val is used. Compiler thinks += is method and tries to find if such a method is declared in immutable Set class. So, it gives a compilation error in case of val.

Immutable Set has no += method declared in it. While mutable Set as += method declared.

Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40