1

How is the ++= being evaluated in the code below? The ++= operator is being applied to a class that is explicitly defined as an immutable list, which does not have the ++= operator. x ++= y appears to have the same behavior as x = x ++ y, but I cannot find where this behavior is coming form. What is happening here? (and where is it documented?)

object ListConcatenation extends App { var list = scala.collection.immutable.List[Int](1) println(list) list = list ++ scala.collection.immutable.List[Int](2) println(list) // This operator does not exist on the List class, but has the same behavior as the operation above. list ++= scala.collection.immutable.List[Int](3) println(list) } 

Output

List(1) List(1, 2) List(1, 2, 3) 
6
  • Isn't it actually two operators? The unary var++ and an assignment with =? Commented May 16, 2016 at 21:57
  • Actually list = list ++ rest. Commented May 16, 2016 at 21:58
  • scala-lang.org/files/archive/spec/2.11/… Commented May 16, 2016 at 21:59
  • @Laurel if I put a space between the ++ and the = operator, then is does not compile. Commented May 16, 2016 at 21:59
  • 1
    Dupe of stackoverflow.com/q/7711481/1296806 Commented May 16, 2016 at 22:06

1 Answer 1

7

x ++= y appears to have the same behavior as x = x ++ y

It does.

but I cannot find where this behavior is coming form.

It's built into the language. When x op= y can't be interpreted as a calling the method op= (because no such method exists in x's class or any class that it can be implicitly converted to), it's instead interpreted as calling the method op and an assignment.

(and where is it documented?)

In Scala's language specification, section 6.12.4:

Assignment Operators

An assignment operator is an operator symbol (syntax category op in Identifiers) that ends in an equals character “=”, with the exception of operators for which one of the following conditions holds:

  1. the operator also starts with an equals character, or
  2. the operator is one of (<=), (>=), (!=).

Assignment operators are treated specially in that they can be expanded to assignments if no other interpretation is valid.

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 

except that the operation's left-hand-side l is evaluated only once.

The re-interpretation occurs if the following two conditions are fulfilled.

  1. The left-hand-side l does not have a member named +=, and also cannot be converted by an implicit conversion to a value with a member named +=.
  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 +.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.