我有下面这段代码,无法编译:
编译器报错:
我错过了什么?
import java.time.Instant
import io.circe.{Decoder, Encoder}
import io.circe.generic.auto._
import io.circe.syntax._
trait SapHealth {}
case class SapHealthRejected(reason: String) extends SapHealth
case class SapHealthAccepted(sapId: String, requestedAt: Long) extends SapHealth
object SapHealth {
private val build: SapHealth = SapHealthAccepted(SapmockActor.system.name, Instant.now().getEpochSecond)
val create: String = build.asJson.noSpaces
implicit val encodeFieldType: Encoder[SapHealthAccepted] =
Encoder.forProduct2("sap-id", "requested_at")(SapHealthAccepted.unapply(_).get)
implicit val decodeFieldType: Decoder[SapHealthAccepted] =
Decoder.forProduct2("sap-id", "requested_at")(SapHealthAccepted.apply)
}
编译器报错:
could not find implicit value for parameter encoder: io.circe.Encoder[com.sweetsoft.SapHealth]
[error] val create: String = build.asJson.noSpaces
我错过了什么?
sealed
关键字的含义是什么? - softshippersealed
关键字使得除了当前编译单元外无法扩展SapHealth
。circe-generic中的泛型派生依赖于Shapeless对ADT的通用表示方式,而Shapeless并没有为非密封的trait继承层次结构提供通用表示方式。 - Travis Brown