September 2018
Intermediate to advanced
398 pages
9h 43m
English
We are now going to explore this variance concept with an example. Let's start with a simple class hierarchy, as shown in the following code:
trait Animalcase class Cat(name: String) extends Animalcase class Dog(name: String) extends Animal
With this hierarchy, I can declare a variable of an Animal type and assign it to an instance of a Cat or Dog type. The following code will compile:
val animal1: Animal = Cat("Max")val animal2: Animal = Dog("Dolly")implicitly[Dog <:< Animal]
More generally, the assignment val a: A = b: B compiles if B <:< A.
Then, we define an InvariantDecoder trait that has a single ...
Read now
Unlock full access