Accepted answer

It's a little tricky to get the H inferred correctly, but you can do it with a <:< instance:

import shapeless._

case class CellEncoder[A](encode: A => String)

implicit val stringCellEncoder: CellEncoder[String] = CellEncoder(identity)
implicit val intCellEncoder: CellEncoder[Int] = CellEncoder(_.toString)

case class Bar(xs: String)

implicit def caseClass1CellEncoder[A, R, H](implicit
  gen: Generic.Aux[A, R],
  ev: R <:< (H :: HNil),
  c: CellEncoder[H]
): CellEncoder[A] = CellEncoder(
  (a: A) => ev( match {
    case h :: t => c.encode(h)

(I've made up a simple CellEncoder for the sake of a complete working example.)

This works because R can be inferred when the compiler is looking for an Generic.Aux[A, R] instance, and can then guide the inference of H when looking for a value for ev.

Related Query

More Query from same tag