简体   繁体   中英

How to Decode a Generic Case Class with semiautomatic in Circe in Scala 3

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?

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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM