Generics, variance problems and bounded types
November 26, 2019 Leave a comment
In scala, generics work for both classes and traits. We can have any number of generic type arguments.
class MyList[A] { def add(element: A): MyList[A] = ??? } val listOfIntegers = new MyList[Int] val listOfStrings = new MyList[String]
// we can have any number of generic type arguments
class MyMap[K, V]
Variance
It defines inheritance relationships of parameterized types. There are 3 types of variance
class Animal class Cat extends Animal class Dog extends Animal
Covariance: List[Cat] can extend from List[Animal]
class CovariantList[+A] // Observe the symbol + val animal: Animal = new Cat val animalList: CovariantList[Animal] = new CovariantList[Cat]
Invariance: List[Cat] cannot extend from List[Animal]
class InvarianceList[A] val invariantAnimalList: InvarianceList[Animal] = new InvarianceList[Animal]
Contravariance: List[Animal] can extend from List[Cat]
class ContraVariantList[-A] val contraVariantList: ContraVariantList[Cat] = new ContraVariantList[Animal] // this may be confusing, look at the below example class Trainer[-A] val contraVariantList1: Trainer[Cat] = new Trainer[Animal] // Now this makes sense as animal trainer can also train cat
Bounded Types
These allow us to use generic classes only for certain types either subtype/supertype of certain types
SubType class Cage[A <: Animal](animal: A) // it says class Cage accepts // only of type A that are sub type of Animal val cage = new Cage(new Dog) class Car // val newCage = new Cage(new Car) // compiler does not // report error but it gets runtime exception SuperType // this enforces super type class Cage1[A >: Animal](animal: A)
Recent Comments