On version:
"org.typelevel" %% "scodec-core" % "1.5.0"
I'm trying to use coproduct functionality, as shown in the test case demonstrate fixing the codec to a known subtype.
I keep getting the error: "could not find implicit value for parameter auto: scodec.codecs.CoproductBuilderAuto[my.class.here]"
I even copy pasted the example and could not get it to work:
import scalaz.\/
import shapeless._
import scodec.bits._
import scodec.codecs._
import scodec._
sealed trait Sprocket
object Sprocket {
implicit val discriminated: Discriminated[Sprocket, Int] = Discriminated(uint8)
}
def codec(d: Int): Codec[Sprocket] = Codec.coproduct[Sprocket].discriminatedBy(provide(d)).auto
I'll continue to look into this on my end, but was wondering if there was an issue fixed around this lately. I cloned the repo, and it worked from the clone - but not when I use the released version.
The error from the example code is caused by not having any subtypes of
Sprocket
defined. If there's at least 1 subtype ofSprocket
, then Shapeless is able to generate aLabelledGeneric[Sprocket]
instance where the representation is a discriminated union. The union is a coproduct of labelled subtypes.Adding the following resolves the error:
Note that you need both the
discriminator
and thecodec
implicits in the companion. If thediscriminator
isn't defined, you'll get the reported error. If thecodec
isn't defined, you'll get a diverging implicit error. Theoretically, theWoozle
codec could be automatically derived if there's an implicitCodec[Int]
in scope, but scalac isn't up to the task -- instead, it bails out with a diverging implicit expansion error. We hope to improve this with Shapeless 2.1.For reference, full source:
And build: