September 2018
Intermediate to advanced
398 pages
9h 43m
English
Let's follow the compiler's advice and create a new CovariantDecoder[+A], along with a CovariantCatDecoder instance that extends it, as shown in the following code:
trait CovariantDecoder[+A] { def decode(s: String): Option[A]}object CovariantCatDecoder extends CovariantDecoder[Cat] { (...)}
We do not show the implementation of decode in CovariantCatDecoder; it is the same as in InvariantCatDecoder. With this covariant parameter, the following relationship is verified:
implicitly[CovariantDecoder[Cat] <:< CovariantDecoder[Animal]]
This time, we can assign the CovariantCatDecoder to an instance of CovariantDecoder[Animal], as shown in the following code:
val covariantAnimalDecoder: CovariantDecoder[Animal] = CovariantCatDecoder ...Read now
Unlock full access