Possible inconsistent error for "+=" shorthand while using immutable set
Christopher Lee <[email protected]> Sun, 27 Mar 2016 10:11:00 -0700 (PDT)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I'm just starting to learn Scala, and I came across the following example
in the "Programming in Scala" book.
var jetSet = Set("Boeing", "Airbus")
jetSet += "Lear"
println(jetSet.contains("Cessna"))
One of the lesson points is that the default Set is immutable. So, I wanted
to test that line 2 in the above actually reassigns a new immutable set
with the element added to the jetSet variable.
I then *changed the var to a val* and expected to see a reassignment error.
Instead I got the below error.
immutableSet.scala:2: error: value += is not a member of scala.collection.
immutable.Set[String]
jetSet += "Lear"
^
While the error message is true, it's strange that the compiler didn't
complain about "+=" before. "+=" is shorthand for `jetSet = jetSet +
"Lear"`, so I would expect to see a reassignment error.
E.g. If I were to run the program with the long form, I get the expected
error message.
val jetSet = Set("Boeing", "Airbus")
jetSet = jetSet + "Lear"
println(jetSet.contains("Cessna"))
immutableSet.scala:2: error: reassignment to val
jetSet = jetSet + "Lear"
^
Am I missing something (very likely since I'm a newb) or does this seem
inconsistent?
Thanks for reading!
Chris
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.