我正在尝试在 Scala 中设置一个使用伴随对象的案例类层次结构。每个案例类都有一个具有隐式方法的伴生对象,但是,我想创建一个案例类可以扩展的抽象类。问题是我无法使抽象类的伴生对象也抽象。下面是代码,我目前有:
abstract class Call {}
abstract class ArgumentsCall extends Call {}
trait Jsonable[T] {
implicit def convert: CodecJson[T]
}
case class VersionCall(version:String) extends Call //{"version": "0.1"}
object VersionCall {
implicit def convert: CodecJson[VersionCall] = casecodec1(VersionCall.apply, VersionCall.unapply)("version")
}
case class CommandCall(command:String,arguments:ArgumentsCall) extends Call //{"command": "pcap-file", "arguments": {"output-dir": "/opt/suricata_out/1", "filename": "/opt/suricata_fifo/1"}}
object CommandCall {
implicit def convert: CodecJson[CommandCall] = casecodec2(CommandCall.apply, CommandCall.unapply)("command","arguments")
}
case class PcapCall(outputDir:String,filename:String) extends ArgumentsCall
object PcapCall extends ArgumentsCall {
implicit def convert: CodecJson[PcapCall] = casecodec2(PcapCall.apply, PcapCall.unapply)("output-dir","filename")
}
case class Response(returnS:String, message:Option[String])
object Response {
implicit def convert: CodecJson[Response] = casecodec2(Response.apply, Response.unapply)("return","message")
}
编译器无法确定 ArgumentsCall 或其子项是否具有“隐式 def convert:CodecJson”。这是实际的编译器错误:
[error] /blah/src/main/scala/helpers/JsonApi.scala:24: could not find implicit value for evidence parameter of type argonaut.EncodeJson[helpers.ArgumentsCall]
[error] implicit def convert: CodecJson[CommandCall] = casecodec2(CommandCall.apply, CommandCall.unapply)("command","arguments")
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
有人可以给我一个很好的方法来推理类/伴随对象以及如何与它们进行继承吗?我有一种感觉,我对如何实现这一点有一个根本的误解。
谢谢