2

Is it possible to overload operator += directly in Scala? It might be useful for some complex types, where a += b may have more efficient and simpler implementation that a = a + b.

The case I encountered recently is I am providing operators for Vector3f from jMonkeyEngine (com.jme.math). A natural implementation could look like:

  import com.jme3.math.{Matrix3f, Vector3f}

  object JMEMathOperators {
    implicit class Vector3fMath(val a: Vector3f) extends AnyVal {
      def - (b: Vector3f) = a subtract b
      def + (b: Vector3f) = a add b
      def * (b: Vector3f) = a mult b
      def * (b: Float) = a mult b

      def += (b: Vector3f) = a addLocal b
    }
  }

In spite of no compile errors, my += implementation is never called. The difference is not that important in this case, addLocal efficiency is only marginally better than add, but one can image there are some complex classes where the difference could be important.

Suma
  • 33,181
  • 16
  • 123
  • 191
  • I was wrong, the += is called. I though it is not because a breakpoint placed on the corresponding line did not trigger, but by placing a println calls into both + and += I can see it is called. – Suma Sep 04 '14 at 14:04
  • 1
    Which IDE? Does removing `extends AnyVal` let you set a breakpoint? – som-snytt Sep 04 '14 at 19:48
  • @som-snytt I am using InteliJ IDEA 13.1 Ultimate with Scala plugin. Yes, after removing "extends AnyVal" the breakpoint hits. – Suma Sep 05 '14 at 18:09

1 Answers1

2

It's certainly possible to provide your own implementation for +=, but I suspect your issue comes from the implicit conversion.

If Vector3f already provides a += method, calling it won't trigger the implicit conversion from Vector3f to Vector3fMath.

For example, if a and b are both Vector3f, doing

a += b

will call the += method on Vector3f, which is completely expected.

In order to trigger the implicit conversion you need to use a method which is not already defined Vector3f. You have to choose another name, perhaps another symbol.


Update

Ok, so Vector3f doesn't define a += (since it's a Java class). Then your code should work fine, the error is probably somewhere else.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • Vector3f is Java object, therefore it certainly does not provide any operators like +=. However you are right the implicit conversion may make the things different here. – Suma Sep 04 '14 at 13:59