For the record, here is the simplest typeafe syntax that I managed to implement:
implicit class Convert[A, RA](value: A)(implicit ga: Generic.Aux[A, RA]) { def convertTo[B, RB](gb: Generic.Aux[B, RB])(implicit ev: RA =:= RB) = gb.from(ga.to(value)) }
And it can be used as follows:
case class Create(userName: String, firstName: String, lastName: String) case class Created(userName: String, firstName: String, lastName: String) val created = Create("foo", "bar", "baz").convertTo(Generic[Created])
Or the same with LabelledGeneric to provide better type safety:
implicit class Convert[A, RA](value: A)(implicit ga: LabelledGeneric.Aux[A, RA]) { def convertTo[B, RB](gb: LabelledGeneric.Aux[B, RB])(implicit ev: RA =:= RB) = gb.from(ga.to(value)) } val created = Create("foo", "bar", "baz").convertTo(LabelledGeneric[Created]))
source share