It is often useful to start talking about hlists when thinking about how you will work with regular value-level lists in a similar situation. For example, suppose we have a value and a list:
val x = 0 val xs = List(1, 2, 3)
And we want to create a new list with x both before and after xs . We can use +: and :+ :
scala> x +: xs :+ x res0: List[Int] = List(0, 1, 2, 3, 0)
Or:
scala> x :: (xs :+ x) res1: List[Int] = List(0, 1, 2, 3, 0)
In the case of Scodec there is no +: operator, but there is :: and :+ , and you can use them in the same way as you would use list versions at the level of values:
import scodec._, scodec.codecs._, shapeless._ def packedByte: Codec[Int :: Int :: Int :: HNil] = uint(4) :: uint(2) :: uint(2) case class MyPacket( foo: Boolean, first: Int, second: Int, third: Int, bar: Boolean ) def packet: Codec[MyPacket] = (bool :: (packedByte :+ bool)).as[MyPacket]
One could build a nested hlist and then flatten it, but :+ much more idiomatic.
source share