Created
February 24, 2011 09:39
-
-
Save ymnk/841977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
samples for demonstrating how implicits are searched. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package object lib { | |
implicit def f2bp(f: Foo) = Bar("package object") | |
} | |
package lib { | |
case class Foo() | |
case class Bar(x: String) | |
object Foo { | |
// implicit def f(f: Foo) = Bar("Foo") | |
} | |
object Bar { | |
// implicit def f(f: Foo) = Bar("Bar") | |
} | |
} | |
package user { | |
import lib.{Foo, Bar} | |
object X { | |
//implicit def f2b0(f: Foo) = Bar("implicit def") | |
//implicit val f2b1 : Foo => Bar = _ => Bar("implicit val") | |
def main(arg: Array[String]): Unit = { | |
implicit def f2b(f: Foo) = Bar("local") | |
println((Foo()):Bar) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package object lib { | |
// implicit val p: Foo = Foo("package object lib") | |
} | |
package lib { | |
case class Foo(x: String) | |
object Foo { | |
implicit val foo: Foo = Foo("package lib") | |
def f(implicit i: Foo) = println(i) | |
} | |
} | |
package user { | |
import lib.Foo | |
import lib.Foo.f | |
trait XX { | |
implicit val xx: Foo = Foo("XX") | |
} | |
object X extends XX { | |
//implicit override val xx: Foo = Foo("X toplevel override") | |
//implicit val toplevel: Foo = Foo("X toplevel") | |
def main(arg: Array[String]): Unit = { | |
//implicit val x = Foo("local") | |
//import Y.y | |
//import Z._ | |
f | |
} | |
} | |
/* | |
object Y { | |
implicit val y: Foo = Foo("Y") | |
} | |
object Z { | |
implicit val z: Foo = Foo("Z") | |
} | |
*/ | |
/* | |
object XXX { | |
def main(arg: Array[String]): Unit = { | |
f | |
} | |
} | |
*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package object user { | |
import lib.Foo | |
implicit val from_package = Foo("package object user") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib { | |
class Boo | |
object Boo{ | |
implicit val g: Foo[Bar] = Foo(Bar("Boo")) | |
} | |
case class Foo[X](x: X) extends Boo | |
case class Bar(x: String) | |
object Bar { | |
// implicit val f1: Foo[Bar] = Foo(Bar("Bar")) | |
} | |
object Foo { | |
// implicit val f0: Foo[Bar] = Foo(Bar("Foo")) | |
} | |
object F { | |
def f(implicit i: Foo[Bar]) = println(i) | |
} | |
} | |
package user { | |
import lib.F.f | |
import lib.{Foo, Bar} | |
object X { | |
def main(arg: Array[String]) = { | |
// implicit val local: Foo[Bar] = Foo(Bar("local")) | |
f | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib { | |
trait MyImplicits { | |
implicit def string2int(x: String) = x.toInt | |
} | |
} | |
package object user extends lib.MyImplicits {} | |
package user { | |
object Test { | |
implicit def string2int = null | |
// implicit def string2intX(x: String) = ("6").toInt | |
def x: Int = "5" | |
def main(arg: Array[String]) = println(x) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib { | |
trait ExecutionStrategy { | |
def exec: Unit | |
} | |
object ExecutionStrategy { | |
implicit object f extends ExecutionStrategy { | |
def exec = println("object ExecutionStrategy") | |
} | |
} | |
object F { | |
def f(implicit f: ExecutionStrategy) = f.exec | |
} | |
} | |
package user { | |
import lib.F.f | |
import lib.ExecutionStrategy | |
object X { | |
implicit val toplevel = new ExecutionStrategy { | |
def exec = println("X toplevel") | |
} | |
def main(arg: Array[String]): Unit = { | |
implicit val local = new ExecutionStrategy { | |
def exec = println("local") | |
} | |
f | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib { | |
class Foo | |
class Bar | |
trait ExecutionStrategy[X] { | |
def exec: Unit | |
} | |
object ExecutionStrategy { | |
implicit object foo extends ExecutionStrategy[Foo] { | |
def exec = println("Foo") | |
} | |
/* | |
implicit object bar extends ExecutionStrategy[Bar] { | |
def exec = println("Bar") | |
} | |
*/ | |
} | |
object Bar { | |
implicit object f extends ExecutionStrategy[Bar] { | |
def exec = println("Bar2") | |
} | |
} | |
object F { | |
def f[X](implicit e: ExecutionStrategy[X]) = e.exec | |
} | |
} | |
package user { | |
import lib.F.f | |
import lib.{ExecutionStrategy, Foo, Bar} | |
object X { | |
implicit val toplevel = new ExecutionStrategy[Foo] { | |
def exec = println("Y toplevel") | |
} | |
def main(arg: Array[String]): Unit = { | |
implicit val local = new ExecutionStrategy[Foo] { | |
def exec = println("local") | |
} | |
f[Foo] | |
f[Bar] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment