2

コンパニオン オブジェクトを使用するケース クラス階層を 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 またはその子が "implicit 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

クラス/コンパニオンオブジェクトについて推論する良い方法と、それらを継承する方法を教えてください。これを実装する方法について根本的な誤解があると感じています。

ありがとう

4

1 に答える 1