Skip to content

Instantly share code, notes, and snippets.

@romac
Last active January 20, 2016 15:07
Show Gist options
  • Save romac/be44f1e2dab0bf03aef9 to your computer and use it in GitHub Desktop.
Save romac/be44f1e2dab0bf03aef9 to your computer and use it in GitHub Desktop.
import shapeless._
import shapeless.ops.product._
import shapeless.syntax.std.product._
case class Foo(a: Int, b: String)
case class Bar(a: Int, b: String, c: Foo)
case class Empty()
case object Object
trait CaseFromProduct[T] {
def apply[P <: Product, L <: HList](p: P)
(implicit genP: Generic.Aux[P, L], genT: Generic.Aux[T, L]): T =
genT.from(p.toHList)
}
object newFoo extends CaseFromProduct[Foo]
object newBar extends CaseFromProduct[Bar]
object newEmpty extends CaseFromProduct[Empty]
object newObject extends CaseFromProduct[Object.type]
val foo = newFoo(42, "hello") // foo: Foo = Foo(42, hello)
val bar = newBar(12, "world", foo) // bar: Bar = Bar(12, world, Foo(42, hello)
val empty = newEmpty // empty: newEmpty.type = ...
val obj = newObject // obj: lObject.type = ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment