Created
May 21, 2016 07:50
-
-
Save duese/84dac439cf1dc6be301b85b31716d5c2 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
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