你能解释一下副产品是如何工作的吗?这是我的代码
sealed trait ArdbData
case class ArdbDataString(value: String) extends ArdbData
case class ArdbDataLong(value: Long) extends ArdbData
case class ArdbDataDouble(value: Double) extends ArdbData
object ArdbDataString {
implicit val codec: Codec[ArdbDataString] = // ... some codec here
implicit val discriminator: Discriminator[ArdbData, ArdbDataString, Int] = Discriminator(6)
}
object ArdbDataLong {
implicit val codec: Codec[ArdbDataLong] = // ... some codec here
implicit val discriminator: Discriminator[ArdbData, ArdbDataLong, Int] = Discriminator(1)
}
object ArdbDataDouble {
implicit val codec: Codec[ArdbDataDouble] = // ... some codec here
implicit val discriminator: Discriminator[ArdbData, ArdbDataDouble, Int] = Discriminator(2)
}
object ArdbData {
implicit val discriminated: Discriminated[ArdbData, Int] = Discriminated(uint8)
import shapeless.syntax.singleton._
// implicit val codec: Codec[ArdbData] = Codec.coproduct[ArdbData].discriminatedBy(uint8).using(
// 'ArdbDataString ->> 4 :: 'ArdbDataLong ->> 1 :: 'ArdbDataDouble ->> 2 :: HNil)
}
我想我必须定义 coproduct 编解码器,ArdbData
如本测试所示。我还需要为我在discriminatedBy.using()
. 当我开始测试它时,我突然发现.using
没有使用来自的密钥。而是使用每个Discriminator
实例的 ID。然后我注释掉Codec.coproduct[ArdbData]
它仍然可以编译!为什么会编译?定义鉴别器价值的正确方法是什么?