elb*_*ich 17
import scala.language.implicitConversions
object ObjA
object ObjB {
def x = 1
}
object Main {
implicit def fromObjA(objA: ObjA.type) = ObjB
def main(args: Array[String]): Unit = {
println(ObjA.x)
}
}
Run Code Online (Sandbox Code Playgroud)
Tom*_*icz 11
隐含地添加方法是什么意思?这段代码是否会回答您的问题:
implicit def toFunkyString(s: String) = new {
def reverseUpper = s.reverse.toUpperCase
}
"Foo".reverseUpper //yields 'OOF'
toFunkyString("Foo").reverseUpper //explicit invocation
Run Code Online (Sandbox Code Playgroud)