Home > Software design >  How to Decode a Generic Case Class with semiautomatic in Circe in Scala 3
How to Decode a Generic Case Class with semiautomatic in Circe in Scala 3

Time:07-18

The following code works with Scala 2.13 (see https://stackoverflow.com/a/59996748/2750966):

import io.circe.generic.semiauto._ 

case class Name(name: String)
case class QueryResult[T: Decoder](data: T)

implicit val nameDer = deriveDecoder[Name]
implicit def result[T: Decoder] = deriveDecoder[QueryResult[T]]

In Scala 3 I get the following Compile Exception:

no implicit argument of type deriving.Mirror.Of[RestEndpoint.this.QueryResult[T]] was found for parameter A of method deriveDecoder in package camundala.bpmn
  implicit def result[T: Decoder]: Decoder[QueryResult[T]] = deriveDecoder[QueryResult[T]]

Is this not yet supported or has there something changed?

CodePudding user response:

It looks like Scala 3 can't generate a Mirror for case classes with multiple parameter lists. I don't know if this is a documented limitation.

Your case class QueryResult has a secondary parameter list because of the context bound on Decoder. Are you sure that you actually need that context bound? Ideally QueryResult shouldn't store any decoders, or even concern itself with decoders at all.

The following works:

import io.circe._
import io.circe.generic.semiauto._ 

case class Name(name: String)
case class QueryResult[T](data: T)

implicit val nameDer: Decoder[Name] =
  deriveDecoder[Name]
implicit def result[T: Decoder]: Decoder[QueryResult[T]] =
  deriveDecoder[QueryResult[T]]

  • Related