scala - Spray Json `RootJsonFormat`'s Invariance? -
given following:
scala> trait parent defined trait parent scala> case class boy(name: string) extends parent defined class boy scala> case class girl(id: int) extends parent defined class girl
i tried define implicit formatter (de-serialization , serialization) them:
scala> object formats extends defaultjsonprotocol { | implicit val format: rootjsonformat[parent] = jsonformat1(boy.apply) | implicit val girlformat: rootjsonformat[parent] = jsonformat1(girl.apply) | }
but got compile-time errors:
<console>:22: error: type mismatch; found : spray.json.rootjsonformat[boy] required: spray.json.rootjsonformat[parent] note: boy <: parent, trait rootjsonformat invariant in type t. may wish define t +t instead. (sls 4.5) implicit val format: rootjsonformat[parent] = jsonformat1(boy.apply) ^ <console>:23: error: type mismatch; found : spray.json.rootjsonformat[girl] required: spray.json.rootjsonformat[parent] note: girl <: parent, trait rootjsonformat invariant in type t. may wish define t +t instead. (sls 4.5) implicit val girlformat: rootjsonformat[parent] = jsonformat1(girl.apply) ^
as understand compile-time error message, rootjsonformat
invariant type parameter.
why that?
Comments
Post a Comment