The scala tutorial says that Int‘s add operation is actually a method call: 1+1 means 1.+(1)
But when I look into the source code of Int.scala, it appears that the method will simply print an error message. Could anyone explain to me how this works?
def +(x: Int): Int = sys.error("stub")
Int is a value class, which is somewhat different than other classes. There is no way to express primitive addition in scala without getting into a recursive definition. For example if the definition of + was,
Then calling + would invoke + which would invoke + which …
Scala needs to compile the methods on value classes into the java byte codes for addition/subtraction/etc.
The compiler does compile + into the java bytecode for addition, but the scala library authors wrote Int.scala with stub methods to make it a valid scala source file. Those stub methods are never actually invoked.