Created
September 28, 2011 04:13
-
-
Save lucastorri/1246969 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
case class Hello { | |
def hello = "hello" | |
} | |
case class Hi { | |
def hi = "hi" | |
} | |
trait Greeting[C] { | |
def greet(c: C): String | |
} | |
case class GreetingCaller[C : Greeting](g: C) { | |
def greet = implicitly[Greeting[C]].greet(g) | |
} | |
object GreetingCaller { | |
implicit def greeting2greetingCaller[C : Greeting](g: C) = GreetingCaller[C](g) | |
} | |
implicit object HelloGreeting extends Greeting[Hello] { | |
def greet(h: Hello) = h.hello | |
} | |
implicit object HiGreeting extends Greeting[Hi] { | |
def greet(h: Hi) = h.hi | |
} | |
import GreetingCaller._ | |
def greetAll[G : Greeting](g: G) = println(g.greet) | |
greetAll(Hello()) | |
greetAll(Hi()) | |
case class FakeHi { | |
def hi = "hi" | |
} | |
greetAll(FakeHi()) //error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment