Created
August 12, 2016 15:58
-
-
Save pjrt/0ce473bf142b2f114397883a2ceb43c5 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
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