Contravariance: intuition building from first principles by Sophie COLLARD
Channel: ScalaIO FR
Date: 2024-03-13

Talk's page 👉 https://scala.io/talks/contravariance-intuition-building

Description:
Contravariance throws many developers off the first time they run into it. This talk will equip the audience with an intuitive understanding of contravariance and the tools for working out from first principles whether a type parameter should be covariant or contravariant.

The goal of this talk is to help the audience build an intuition for contravariance from first principles, instead of relying on memorised shortcuts such as “input type parameters tend to be contravariant and output type parameters covariant”.

This talks is designed for beginner to intermediate Scala developers. Attendees need to be familiar with subtyping and typeclasses. An understanding of covariance would be beneficial but is not required as we’ll cover it in the talk.

We’ll begin by working with an Animal type hierarchy and some PetRescue and PetClinic typeclasses. By working out what can be substituted for what, we’ll begin building up intuition for covariance and contravariance. We’ll then test this intuition by working through a second example involving JsonDecoder and JsonEncoder typeclasses.