Skip to content

Instantly share code, notes, and snippets.

@duese
Created May 21, 2016 07:50
Show Gist options
  • Save duese/84dac439cf1dc6be301b85b31716d5c2 to your computer and use it in GitHub Desktop.
Save duese/84dac439cf1dc6be301b85b31716d5c2 to your computer and use it in GitHub Desktop.
trait Util {
trait AttributeResolver[T] {
def get(): T
}
implicit object StringResolver extends AttributeResolver[String] {
def get(): String = "Alpha"
}
implicit object IntResolver extends AttributeResolver[Int] {
def get(): Int = 47
}
def get[T]()(implicit resolver: AttributeResolver[T]): T = resolver.get()
// alternatively:
//def get[T: AttributeResolver](): T = implicitly[AttributeResolver[T]].get()
}
object Foo extends Util {
def main (args: Array[String]) {
val x: String = get()
//val x: Int = get()
//val x: Double = get()
println(x)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment