2

When defining my messages with scodec, I would like to use nested case classes. For example:

case class Foo(x: Int, y: Int)
object Foo {
  def baseCodec = uint16 :: uint16
  def codec = baseCodec.as[Foo]
}

case class Bar(a: Int, foo: Foo, b: Int)
object Bar {
  val baseCodec = uint8 :: Foo.baseCodec :: uint16
  val codec = baseCodec.as[Bar]
}

However, when trying to compile this I get the following:

 error: Could not prove that shapeless.::[Int,shapeless.::[shapeless.::[Int,shapeless.::[Int,shapeless.HNil]],shapeless.::[Int,shapeless.HNil]]] can be converted to/from Bar.
         val codec = baseCodec.as[Bar]
                                 ^

Is there a way of doing this? (In my real code, sometimes the nested case class appears at the beginning of the containing class' parameter list, sometimes in the middle and sometimes at the end).

Mario Camou
  • 2,303
  • 16
  • 28

1 Answers1

3

I figured it out. You need to use the Codec, not the HList. This works:

object Bar {
  //Foo.codec instead of Foo.baseCodec
  val baseCodec = uint8 :: Foo.codec :: uint16
  val codec = baseCodec.as[Bar]
}
Travis Stevens
  • 2,198
  • 2
  • 17
  • 25
Mario Camou
  • 2,303
  • 16
  • 28