1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
import scala.language.implicitConversions
object A {
object B {
implicit def int2string(x: Int) = "["+x.toString+"]"
}
}
class C(x: String) {
class Inner {
}
object Inner {
val s: String = x
implicit def Inner2String(x: Inner): String = s
}
}
object Test extends App {
import A.B._
val c = new C("OK")
val i = new c.Inner
val s: String = i
Console.println(s)
Console.println(2: String)
}
object TestPriority {
class C(x: Int) { def foo: Int = x + 1 }
class D(x: Int) { def foo: Int = x + 2 }
class IMPL {
implicit def Int2C(x: Int): C = new C(x)
}
object impl extends IMPL {
implicit def Int2D(x: Int): D = new D(x)
}
import impl._
val x: C = 2
val y: D = 2
assert(x.foo == 3, x.foo)
assert(y.foo == 4, y.foo)
assert((2).foo == 4, (2).foo)
}
|