Last active
December 12, 2015 04:48
-
-
Save tpolecat/4716494 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| object SJ extends App { | |
| trait Getter[T] { | |
| def get(s: String): T | |
| } | |
| object Getter { | |
| implicit val getInt = new Getter[Int] { | |
| def get(s: String) = s.toInt // unsafe | |
| } | |
| implicit val getString = new Getter[String] { | |
| def get(s: String) = s | |
| } | |
| } | |
| def get[T: Getter](s: String): T = implicitly[Getter[T]].get(s) | |
| case class A(s: String) | |
| case class B(n: Int) | |
| class Foo[T: Getter](s: String) { | |
| val t: T = get(s) | |
| } | |
| // no compile (ambiguous implicits) | |
| // A(get("foo")) | |
| // B(get("foo")) | |
| A(get[String]("foo")) | |
| B(get[Int]("foo")) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment