3

I had defined a val variable (t), and an array(m) with Int values and then tried to perform sum of all elements of array using for loop in two ways: case1. Using += (Error message: value += is not a member of Int ) case2. Using a=x+y way (Error message: reassignment to val )

Error is expected in this case as I'm trying to re-assign a new value to a val variable but why there is different error message in case1 and case2?

scala> val t = 0                                                                                                                              
t: Int = 0 

scala> val m = Array(1,2,3,4,5,6,7)                                                                                                           
n: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)  

case1:

scala> for(e<-m) t+=e                                                                                                                         
<console>:28: error: value += is not a member of Int                                                                                          
       for(e<-m) t+=e                                                                                                                         
                  ^   

case2:

scala> for(e<-m) t=t+e                                                                                                                        
<console>:28: error: reassignment to val                                                                                                      
       for(e<-m) t=t+e                                                                                                                        
                  ^
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
MurthyD
  • 41
  • 4
  • 2
    Does the last bullet point in [this answer](https://stackoverflow.com/a/2662998) solve your problem? – Andrey Tyukin Jun 03 '19 at 18:07
  • @AndreyTyukin I don't think that point answers my question. As += (case1) works successfully if I declare the variable "t" as var. Also in the example given at the link - a is an object of type var. – MurthyD Jun 03 '19 at 18:17
  • 1
    What's the problem with a snippet of code working successfully? The linked answer explains precisely why `+=` works on an `Int` var. Also, `var` is not a type. – Andrey Tyukin Jun 03 '19 at 18:18
  • 2
    Maybe I'll summarize it: 1) If `t` is a `val` of type `Int`, then `t += ...` produces an error, because `Int` has no method called `+=`. 2) If `t` is a `val`, then reassignment to `t` is not allowed, because that's the whole point of `t` being a `val`. 3) If `t` is not a `val`, then `+=` is desugared as explained in the linked answer. Every case mentioned so far seems to be precisely described either by the error message itself, or by the linked duplicate answer. – Andrey Tyukin Jun 03 '19 at 18:24

1 Answers1

2

Consider the desugared version of t += 42 when t is a val:

t.$plus$eq(42)

Note how there is no assignment happening, instead, it is simply a method call on t. Contrast this with desugared version of t += 42 when t is a var and it does not have += method available

t = t.$plus(42)

Here we see an assignment statement. Therefore, as there is no assignment happening in the case of t+=e where t is a val, the error message does not indicate reassignment to val, instead it is complaining about missing method +=, that is, $plus$eq, on t.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • So, `+` desugars into `$plus` at some point. Good. It's not quite clear what the replacement of `+` by `$plus` buys us, exactly. – Andrey Tyukin Jun 03 '19 at 18:29
  • `t.$plus$eq(42)` shows there is no assignment happening, which perhaps explains different compiler message. – Mario Galic Jun 03 '19 at 18:31
  • 1
    Ok, yes... *"If you invoke method `m` on object `t` with argument `a`, then `t.m(a)` is not rewritten into an assignment"* - That's true, but it leaves too many things implicit. If one doesn't expect that certain methods indeed *are* rewritten into assignments, it reads like a tautological truism. On the other hand, if one already knew that the method `+=` sometimes rewrites into an assignment, then one wouldn't need this Q/A... Maybe you should add the third case (`var t`, `+=`) mentioned in the comments, for contrast. – Andrey Tyukin Jun 03 '19 at 18:34
  • 1
    1. I'd suggest either desugaring to `$plus` in both cases or to `.+=(42)` in the first. 2. "when t is a var" That's not correct. If there is a `+=` method, it'll be used for a `var` as well. – Alexey Romanov Jun 04 '19 at 06:54