Created
August 20, 2014 16:20
-
-
Save travisbrown/2bcee3650447094059e2 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
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