Skip to content

Instantly share code, notes, and snippets.

@tomverran
Last active March 14, 2017 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomverran/53e356faca84561953e7d2393c15fda1 to your computer and use it in GitHub Desktop.
Save tomverran/53e356faca84561953e7d2393c15fda1 to your computer and use it in GitHub Desktop.
This hangs the Scala compiler (tried 2.11.8 and 2.12.1)
import shapeless.{:+:, CNil, Coproduct, HList, LabelledGeneric}
import shapeless.ops.hlist.{Align, Intersection}
/**
* Allows you to convert between case classes which have compatible generic representations
* i.e. Foo(bar: String) is convertable to Baz(bar: String) due to the field names & types being the same
*/
trait Converter[A, B] {
def convert(a: A): B
}
object Converter {
implicit class ConverterOps[A](a: A) {
def convert[B](implicit c: Converter[A, B]) = c.convert(a)
}
implicit def coproductConvert[H, T <: Coproduct, B](
implicit
h: Converter[H, B],
c: Converter[T, B]
) = new Converter[H :+: T, B] {
override def convert(a: :+:[H, T]) = a.eliminate(h.convert, c.convert)
}
implicit def cnilConvert[B] = new Converter[CNil, B] {
override def convert(a: CNil) = throw new Exception("Impossible")
}
implicit def convert[A, R1 <: HList, B, R2 <: HList, I <: HList](
implicit
l1: LabelledGeneric.Aux[A, R1],
l2: LabelledGeneric.Aux[B, R2],
s: Intersection.Aux[R1, R2, I],
e: Align[I, R2]
) = new Converter[A, B] {
override def convert(a: A) = {
l2.from(e(s(l1.to(a))))
}
}
}
@tomverran
Copy link
Author

Turns out it is the use of Intersection causing the hang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment