2

Tutorial mentions about mutable sets in the initial but why would the REPL change the instance name from res4 to res5 when a new element is added? Is 'res' not the instance name that REPL prints? Below is the code in context. Beginner in scala. Please bear if the question is trivial.

scala> val set = scala.collection.mutable.Set[Int]()
val set: scala.collection.mutable.Set[Int] = Set()

scala> set += 1
val res0: scala.collection.mutable.Set[Int] = Set(1)

scala> set += 2 += 3
val res1: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
Andronicus
  • 25,419
  • 17
  • 47
  • 88
nashter
  • 1,181
  • 1
  • 15
  • 33
  • 2
    No, the REPL prints the variable names. It generates new names for the unnamed expressions. Multiple variables can point to the same object. – Gábor Bakos Apr 13 '20 at 05:11

1 Answers1

4

The reference did not change though, it means res0 == res1. Scala repl will generate names for expressions that are not assigned any name, no matter if it's mutable or not.

Additionally take a look at the docs. For mutable.Set, the method += results in Set.this.type. Since there is a value returned, it has to be assigned some name.

Andronicus
  • 25,419
  • 17
  • 47
  • 88