Skip to content

Instantly share code, notes, and snippets.

@lucastorri
Created September 28, 2011 04:13
Show Gist options
  • Save lucastorri/1246969 to your computer and use it in GitHub Desktop.
Save lucastorri/1246969 to your computer and use it in GitHub Desktop.
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