現在、次のように見えるいくつかのモデルをリファクタリングしようとしています。
case class Person(name: String, age: Int)
object Person {
implicit val reads: Reads[Person] = (
(JsPath \ "name").read[String] and
(JsPath \ "age").read[Int]
)(Person.apply _)
}
このようなものに:
abstract class BaseModel {
val pk: String // Some stuff here that will be common
}
object BaseModel {
implicit val reads: Reads[BaseModel] // Not sure what to do here
}
私がこれを行うことができるように:
trait MyTrait[Model <: BaseModel] {
// More code here
val body: JsObject = ...
val parsed = body.validate[Model] // Error: There is no implicit value defined for Model
}
case class NewPerson extends BaseModel {...}
object NewPerson {...} // Maybe need to extend something here
class MyController extends MyTrait[NewPerson]
すべてのモデルで暗黙的な読み取り値を定義する必要がありますが、抽象クラスのコンパニオン オブジェクトでこれを示す方法がわかりません。