Skip to content

Instantly share code, notes, and snippets.

@utaal
Last active November 16, 2016 13:18
Show Gist options
  • Save utaal/981c4522c69fd7863a16b7461c46ccb0 to your computer and use it in GitHub Desktop.
Save utaal/981c4522c69fd7863a16b7461c46ccb0 to your computer and use it in GitHub Desktop.

This works:

scala> Typeclass[ADT].adtName(One)
res1: String = hi

However, this doesn't:

scala> Typeclass[One.type]
<console>:12: error: could not find implicit value for parameter evidence: Typeclass[One.type]
       Typeclass[One.type]

even though this does:

scala> implicit val typeclassForADT = Typeclass.typeclassFor[ADT]

scala> Typeclass[One.type]
res3: Typeclass[One.type] = Typeclass$$anon$1@3430e6a5
sealed abstract trait ADT
case object One extends ADT
case object Two extends ADT
scalaVersion := "2.12.0"
crossScalaVersions := Seq("2.11.8", "2.12.0")
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
trait Typeclass[-A] {
def something(a: A): String
}
object Typeclass {
implicit def typeclassFor[T](implicit ev: T =:= ADT): Typeclass[T] = new Typeclass[T] {
def something(a: T): String = "hi"
}
def apply[A](implicit evidence: Typeclass[A]) =
evidence
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment