Contravariance

object ContravarianceTest extends App {
  val myCat: Cat = Cat("Boots")

  def printMyCat(printer: Printer[Cat]): Unit = {
    printer.print(myCat)
  }

  val catPrinter: Printer[Cat] = new CatPrinter
  val animalPrinter: Printer[Animal] = new AnimalPrinter

  printMyCat(catPrinter)
  printMyCat(animalPrinter)
}

A type parameter A of a generic class can be made contravariant by using the annotation -A.

Contravariance → is a → Variance.

Related concepts

Contravariance

Contravariance — Structure map

Clickable & Draggable!

Contravariance — Related pages: