Vladimir Kirichenko-2 wrote:
One of the typical uses of implicit conversions is to declare
"additional" methods for the existing class. Compiler substitutes
reference to object with invocation of implicit function that typically
instantiates some kind of RichObject or anonymous object with additional
functions. For example
var i = 5;
i = i max 6;
i = i max 7;
will be compiled to 2 instantiations of RichInt. The use of anonymous
declaration in implicit will cause big performance degradation.
Also in these cases usually impossible to reuse created instance.
The idea is to create some kind of "implicit traits" for this kind of
situations that would not cause performance issues and also eliminates
the need of implicit def declarations:
implicit[Int] trait RichInt {
def max(x: Int) = this > x
def min(x: Int) = this < x
}
and the
i = i max 6;
will actually be compiled to "static" method invocation
i = RichInt.max(i, 6);
...
IMHO it would make more sense to allow an "inversion" of the first element of a parameterlist in order to preserve performance, like in the Nice language:
[code]
//pseudocode
inversion def max(x:Int, y:Int) = if (x > y) x else y
val m = 10.max(20)
//or using the infix notation
val m = 10 max 20
[/code]