Scala implicit scope requires double import
I can't see why in the following code the import (import b._
) that pulls in the implicit def
must appear in both position 1 and position 2 for it to work.
package a {
abstract class Base {}
}
package b {
import a._
class Derived(i: Int) extends Base {}
object b {
implicit def 开发者_如何学Ci2d(i: Int): Derived = new Derived(i)
}
}
import a._
// position 1
import b._
object test extends App {
// position 2
import b._
def doIt(base: Base) {
println("works")
}
doIt(1)
}
At position 1 you are importing everything from the package b and at position 2 you are importing everything from the object b, which includes the implicit def. You could just import b.b._
at position 2.
精彩评论