Re: Possible inconsistent error for "+=" shorthand while using immutable set

Andrew Phillips <[email protected]> Sun, 27 Mar 2016 11:03:14 -0700 (PDT)
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
Hi Chris

The behaviour you're seeing is indeed consistent with the language spec, 
although I can imagine that it appears odd at first sight. In short, the 
rewriting of a += b to a = a + b can only take place if a is something 
"that can be assigned". Since that's not true for a val, the rewriting does 
*not* take place, and so the compiler is forced to look for a member "+=" 
on a instead. Since a in your example is an immutable set, it can't find 
one (mutable sets *do* have a "+=" method [1]).

Section 6.12.4 of the language spec [2] describes this in detail; here an 
excerpt:


Let’s consider an assignment operator such as += in an infix operation l += 
r ,
where l, r are expressions. This operation can be re-interpreted as an 
operation
which corresponds to the assignment

l = l + r

[...] if the following two conditions are fulfilled.

[...]

2. The assignment l = l + r is type-correct. In particular this implies 
that l
refers to a variable or object that can be assigned to, and that is 
convertible
to a value with a member named +.


Hope that helps!

ap

[1] http://www.scala-lang.org/api/current/index.html#scala.collection.mutable.Set@+=(elem:A):SetLike.this.type
[2] http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#assignment-operators

On Sunday, 27 March 2016 18:11:00 UTC+1, Christopher Lee wrote:
>
> 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.