Variances in Scala: What is Variance?

Variances in Scala: What is Variance?

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.

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: covarianceinvariance *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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Partial Functions in Scala

Learn what Partial Functions are and how to use them in Scala. See the difference between map and collect when using those on collection.

Higher Order Functions (HOF) in Scala

Learn what are and how to use Higher Order Functions (HOF) in Scala. Leverage best functional programming paradigms in your daily workflow.

Understanding Higher-Order functions in Scala

Reading Time: 4 minutes A blog on using higher-order functions in Scala.A higher-order function is a function that takes in another function as argument and itself returns some value or function. Some examples of higher-order functions include map, filter, reduce, foreach etc.

Functional Programming

Functional Programming: Functional Programming is a Declarative style of Programming Paradigm for writing computer programs.

Are functions from programming really functions?

Are functions from programming really functions? If you are reading this, then most probably you already know quite well what functions are in programming.