I have always interpreted the specification as meaning that the implicit can be defined in the companion object of the implicit parameter, and not in the class containing the definition. Something like that:
object ZZ { implicit val xyz: ZZ = new ZZ() } class ZZ { def bar: (String) => Unit = null } class XX { def foo(s: String)(implicit f: ZZ): Unit = { if (f.bar == null) println("Just: " + s) else f.bar(s) } def bar { foo("xyz") } }
As can be seen from section 7.2 of the specification:
The actual arguments that are allowed to pass an implicit parameter of type T fall into two categories. Firstly, all identifiers x that can be obtained at the point of method invocation without prior identification and which indicate an implicit definition (§7.1) or an implicit parameter are suitable. a suitable identifier, therefore, is the local name or member of the attached template, or it can be made available without prior (§4.7). If there are no suitable identifiers according to this rule, then, secondly, the right to participate is also implicit members of some object that belongs to the implicit volume type of implicit parameters, T.
Can you quote the part that indicates the companion object of the containing definition class?
Mitch blevins
source share