This answer is delayed for 2 years, but for others I would like to indicate that the accepted answer is unnecessarily distributed from AnyVal.
There is only a minor bug that needs to be fixed in the original answer. The def ** method requires only one parameter, that is, an exponent, since the base is already passed in the constructor, and not as in the source code. Fixing this and removing the backward steps results in:
import scala.math.pow implicit class PowerInt(i: Int) { def ** (b: Int): Int = pow(i, b).intValue }
How it works here .
The Scala compiler will distinguish Int to PowerInt only if the method that is called on it is undefined. That is why you do not need to exit AnyVal.
Behind the scenes, Scala is looking for an implicit class whose constructor argument type matches the type of object that is being executed. Since an object can have only one type, implicit classes cannot have more than one argument in its constructor. Moreover, if you define two implicit classes with the same constructor type, make sure that their functions have unique signatures, otherwise Scala would not know which class would belong and complain about ambiguity.
Swayam
source share