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.

What is Variance?

Variance is the correlation of subtyping relationships of complex types and the subtyping relationships of their component types.

Scala-lang.org

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.

Covariance

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

Variances in Scala: What is Variance?
2.10 GEEK