Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is the + in += on a Map a prefix operator of =?

Tags:

scala

In the book "Programming in Scala" from Martin Odersky there is a simple example in the first chapter:

var capital = Map("US" -> "Washington", "France" -> "Paris")
capital += ("Japan" -> "Tokyo")

The second line can also be written as

capital = capital + ("Japan" -> "Tokyo")

I am curious about the += notation. In the class Map, I didn't found a += method. I was able to the same behaviour in an own example like

class Foo() {
    def +(value:String) = {
        println(value)
        this
    }
}

object Main {
  def main(args: Array[String]) = {
   var foo = new Foo()
   foo = foo + "bar"
   foo += "bar"
  }
}

I am questioning myself, why the += notation is possible. It doesn't work if the method in the class Foo is called test for example. This lead me to the prefix notation. Is the + a prefix notation for the assignment sign (=)? Can somebody explain this behaviour?

like image 339
Steve Avatar asked May 06 '10 07:05

Steve


2 Answers

If you have a symbolic method that returns the same object, then appending equals will perform the operation and assignment (as a handy shortcut for you). You may also always override the symbolic method. For example,

scala> class Q { def ~#~(q:Q) = this }
defined class Q

scala> var q = new Q
q: Q = Q@3d511e

scala> q ~#~= q
like image 135
Rex Kerr Avatar answered Nov 08 '22 14:11

Rex Kerr


// Foo.scala
class Foo {
  def +(f: Foo) = this
}

object Foo {
  def main(args: Array[String]) {
    var f = new Foo
    f += f
  }
}

Output of scalac -print Foo.scala:

package <empty> {
  class Foo extends java.lang.Object with ScalaObject {
    def +(f: Foo): Foo = this;
    def this(): Foo = {
      Foo.super.this();
      ()
    }
  };
  final class Foo extends java.lang.Object with ScalaObject {
    def main(args: Array[java.lang.String]): Unit = {
      var f: Foo = new Foo();
      f = f.+(f)
    };
    def this(): object Foo = {
      Foo.super.this();
      ()
    }
  }
}

As you can see the compiler simply converts it to an assignment and a call of the method.

like image 39
michael.kebe Avatar answered Nov 08 '22 14:11

michael.kebe