简体   繁体   English

如何在 Scala 3 中的 Circe 中使用半自动解码通用案例类

[英]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 ):以下代码适用于Scala 2.13 (请参阅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:Scala 3中,我得到以下编译异常:

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.看起来 Scala 3 无法为具有多个参数列表的案例类生成Mirror 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 .由于Decoder上的上下文绑定,您的案例类QueryResult有一个辅助参数列表。 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.理想情况下QueryResult不应该存储任何解码器,甚至根本不关心解码器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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