Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Created August 20, 2014 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travisbrown/2bcee3650447094059e2 to your computer and use it in GitHub Desktop.
Save travisbrown/2bcee3650447094059e2 to your computer and use it in GitHub Desktop.
sealed trait A {
type Typeclass[T] <: { def apply(): Unit }
}
case class B() extends A {
trait Typeclass[T] { def apply(): Unit }
object Typeclass {
implicit val intClass: Typeclass[Int] = new Typeclass[Int] {
def apply() { println("B.Int") }
}
implicit val longClass: Typeclass[Long] = new Typeclass[Long] {
def apply() { println("B.Long") }
}
}
}
case class C() extends A {
trait Typeclass[T] { def apply(): Unit }
object Typeclass {
implicit val intClass: Typeclass[Int] = new Typeclass[Int] {
def apply() { println("C.Int") }
}
}
}
// The Singleton part is needed to keep D(b) from compiling.
case class D[X <: A with Singleton](a: X) {
def hey[T](implicit imp: X#Typeclass[T]) { imp() }
}
val b = B()
val d = D[b.type](b)
d.hey[Int]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment