Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Last active December 12, 2015 04:48
Show Gist options
  • Select an option

  • Save tpolecat/4716494 to your computer and use it in GitHub Desktop.

Select an option

Save tpolecat/4716494 to your computer and use it in GitHub Desktop.
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