According to the Scala Spec (2.8), for an implicit to be found it must be defined in local scope, inherited scope, or in a companion object. Given that, it seems to me that the following code should work without an explicit import of the contents of the companion object. I see this used in the Scala library source (eg. CanBuildFrom). It also seems that I should be able to call XX.foo() from outside the definition of the XX class and have my implicit parameter from the companion class used. What am I missing?
object XX {
implicit def XYZ[T]: (T) => Unit = null
}
class XX {
// import XX._ // Works with this line uncommented...
def foo(s: String)(implicit f: (String) => Unit): Unit = {
if (f == null)
println("Just: " + s)
else
f(s)
}
def bar {
foo("abc"){ s => println("Func: " + s)}
foo("xyz") // <-- Compile error here: could not find implicit value for parameter f
}
}