Scala supports using type parameters to implement a classes and functions that can support multiple types. Type parameters are very useful when creating a generics. However, advanced use cases may require you to specify a constraints on types used. That’s where a Variances help to model the relationships between generic types. This post will cover the topic of type variances in Scala.
Variance is the correlation of subtyping relationships of complex types and the subtyping relationships of their component types.
In other words, variance allows developers to model the relationships between types and component types. As a consequence, variance allows to create a clean and very usable generic classes that are widely used in various Scala libraries.
Scala supports thee types of variance: covariance, **invariance **and contravariance. With this in mind, let’s look at each of these in greater detail.
Let’s assume that we have the following class structure
abstract class Vehicle {
def name: String
}
case class Car(name: String) extends Vehicle
case class Bus(name: String) extends Vehicle
class VehicleList[T](val vehicle: T)
The Car
and Bus
classes both inherit from abstract class Vehicle
. Considering that class Car
has its own collection VehicleList[Car]
: is VehicleList[Car]
a subtype of VehicleList[Vehicle]
? The answer is no. Despite the fact that Car
class extends a Vehicle
class, the same can’t be said about VehicleList[Car]
and VehicleList[Vehicle]
.
val vehicleList: VehicleList[Vehicle] = new VehicleList[Car](new Car("bmw")) // incorrect: type mismatch
#jvm #coding #programming #scala #function