Reputation: 33598
If I write expression 1+2
in scala, it means that actually +
method call on object 1
.
But how is +
function implemented? If it like this:
public Int + (one: Int, two: Int) {
return one + two
}
//Sorry if syntax is not well correct
it leads to infinite recursion because +
is function and calls itself.
So logically, there must be a way to tell scala do "native" addition operation instead of +
function call.
How to do that?
Upvotes: 0
Views: 56
Reputation: 32335
The +
method is an intrinsic method -- it is translated by the Scala compiler specially. The Scala compiler rewrites +
method calls to addition instructions in bytecode.
1 + 2
becomes (in bytecode):
iconst_1
iconst_2
iadd
Upvotes: 4