Skip to content

Instantly share code, notes, and snippets.

@pierzchalski
Created November 7, 2014 10:33
Show Gist options
  • Save pierzchalski/6df04208171472b04f7d to your computer and use it in GitHub Desktop.
Save pierzchalski/6df04208171472b04f7d to your computer and use it in GitHub Desktop.
TypeClassCompanion import issues
import shapeless._
import shapeless.test._
sealed trait T
case class A(s1: String, t: T) extends T
case class B(s2: String) extends T
object Demo1 {
import DemoAux._
def main(args: Array[String]): Unit = {
// This is the primary example: if the `TypeClassCompanion` trait is
// not on the companion object of the type class, this results.
import SeparateTC._
illTyped("implicitly[Separate[T]]")
import SeparateTC.auto._
implicitly[Separate[T]]
// Turns out you can't use two autos in the same scope, either
import Simple._
import Simple.auto._
illTyped("implicitly[Simple[T]]")
// We can still make things though
val v1 = Simple[T]
val v2 = Simple.auto.derive[T]
}
}
object Demo2 {
import DemoAux._
def main(args: Array[String]): Unit = {
// For type classes where the `TypeClassCompanion` is on the typeclass companion,
// things are rather pleasant.
import Simple.auto._
implicitly[Simple[T]]
// For other things...
import SeparateTC.auto._
illTyped("implicitly[Separate[T]]")
import SeparateTC._
illTyped("implicitly[Separate[T]]")
// We're having to get very explicit by now, because of the
// 'two autos in scope' problem mentioned above
implicit val v = SeparateTC.auto.derive[T]
implicitly[Separate[T]]
}
}
object DemoAux {
trait Simple[A] {
def m(a: A): String
}
// We care about implicit resolution currently, not implementation
object Simple extends LabelledTypeClassCompanion[Simple] {
implicit def t1str: Simple[String] = ???
implicit val t1c: LabelledTypeClass[Simple] = new LabelledTypeClass[Simple] {
def coproduct[L, R <: Coproduct](name: String, CL: => Simple[L], CR: => Simple[R]): Simple[:+:[L, R]] = ???
def emptyCoproduct: Simple[CNil] = ???
def emptyProduct: Simple[HNil] = ???
def product[H, T <: HList](name: String, CHead: Simple[H], CTail: Simple[T]): Simple[::[H, T]] = ???
def project[F, G](instance: => Simple[G], to: (F) => G, from: (G) => F): Simple[F] = ???
}
}
trait Separate[A] {
def m(a: A): String
}
object Separate {
implicit def t2str: Separate[String] = ???
}
object SeparateTC extends LabelledTypeClassCompanion[Separate] {
implicit val t2c: LabelledTypeClass[Separate] = new LabelledTypeClass[Separate] {
def coproduct[L, R <: Coproduct](name: String, CL: => Separate[L], CR: => Separate[R]): Separate[:+:[L, R]] = ???
def emptyCoproduct: Separate[CNil] = ???
def emptyProduct: Separate[HNil] = ???
def product[H, T <: HList](name: String, CHead: Separate[H], CTail: Separate[T]): Separate[::[H, T]] = ???
def project[F, G](instance: => Separate[G], to: (F) => G, from: (G) => F): Separate[F] = ???
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment