DEV Community

Cover image for *Variance Analogy
Montegasppα Cacilhας
Montegasppα Cacilhας

Posted on

4

*Variance Analogy

Post from Kodumaro.

German Shepherd dog

The other day, talking to some coworkers, I realised developers unacquainted to functional programming find hard to understand type covariance and contravariance abstractions.

So I thought about an analogy to make it easier.

Type contravariance

Let’s define two classes that represent something easy to get in mind: dogs. Shepherds are dogs, so (in Scala):

class Dog

class Shepherd extends Dog
Enter fullscreen mode Exit fullscreen mode

We have a job for a dog trainer. Think about a Shepherd pack needing to be trained.

The job title is “Shepherd trainer”, but a dog trainer – someone who know how to train every kind of dog – is competent for the job:

class Trainer[-A]

val trainerJob: Trainer[Shepher] = new Trainer[Dog]
Enter fullscreen mode Exit fullscreen mode

That’s okay! But a Shepherd trainer couldn’t take a general dog trainer job, ’cause he has no experience with training other kinds of dog.

This is contravariance:

// If:
implicitly[Shepherd <:< Dog]
// Then:
implicitly[Trainer[Dog] <:< Trainer[Shepherd]]
Enter fullscreen mode Exit fullscreen mode

<:< means “is subclass of,” or: every Shepherd is a Dog too, so every Trainer[Dog] is a Trainer[Shepherd] too.⦆

In Scala we use - to represent contravariance.

Type covariance

Now imagine that we need to hire a dog producer. We’re not interested in what kind of dog he’ll supply, any kind is great.

So a Shepherd producer could apply for the service, ’cause Shepherd dogs are so good as any other.

class Producer[+A]

val producerContract: Producer[Dog] = new Producer[Shepherd]
Enter fullscreen mode Exit fullscreen mode

It’s called covariance:

// If
implicitly[Shepherd <:< Dog]
// Then
implicitly[Producer[Shepherd] <:< Producer[Dog]]
Enter fullscreen mode Exit fullscreen mode

⦅Yet every Producer[Shepherd] is a Producer[Dog] too – he produces only dogs.⦆

In Scala we use + to represent covariance.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay