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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -