Skip to content

Instantly share code, notes, and snippets.

@pjrt
Created August 12, 2016 15:58
Show Gist options
  • Save pjrt/0ce473bf142b2f114397883a2ceb43c5 to your computer and use it in GitHub Desktop.
Save pjrt/0ce473bf142b2f114397883a2ceb43c5 to your computer and use it in GitHub Desktop.
trait TypeclassA[A] { }
trait TypeclassB[A] { }
trait TypeclassC[A] {
implicit val typeAInstance: TypeclassA[A]
implicit val typeBInstance: TypeclassB[A]
}
object TypeclassC {
/**
* Helper function for creating TypeclassC instances
*/
def mkInstance[A](implicit ev1: TypeclassA[A], ev2: TypeclassB[A]) = new TypeclassC[A] {
implicit val typeAInstance = ev1
implicit val typeBInstance = ev2
}
}
object Test {
import TypeclassC.mkInstance
implicit val tAInt: TypeclassA[Int] = new TypeclassA[Int] {}
implicit val tBInt: TypeclassB[Int] = new TypeclassB[Int] {}
implicit val tCInt = mkInstance[Int] // compiles
implicit val tCString = mkInstance[String] // does NOT compile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment