And currently it does not work with implicits
There’s a PR to fix this: Strengthen overloading resolution to deal with extension methods by odersky · Pull Request #6116 · lampepfl/dotty · GitHub
In a nutshell overloading rules are as follows:
Given a call x.m(args)
- all members of
x
are considered first. - if no member matches, all imported extension methods are considered next.
- if none of these matches either, we search for an implicit value that contains an extension method
m
. If a unique value is found, we consider its extension methods, which might again have overloaded variants form
.
Overloaded extension methods cannot be obtained from several implicit values. E.g. this code would
give an implicit ambiguity error:
implied A {
def (x: Int) |+| (y: Int) = x + y
}
implied B {
def (x: Int) |+| (y: String) = x + y.length
}
assert((1 |+| 2) == 3) // error: ambiguous
However, one can always disambiguate in this case by importing one extension method which then takes precedence.