Skip to content

Variance rewrite #2488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2022
Merged

Variance rewrite #2488

merged 3 commits into from
Aug 26, 2022

Conversation

martijnhoekstra
Copy link
Contributor

All but a full rewrite. I have always found this section of the tour confusing at least, particularly the part where it does the function.

In my experience, most people start with the false intuition that things should automatically be invariant. That's why I start with invariance, show why things can't be covariant by default. Then I show covariance, and then I show contravariance as the opposite.

I completely omit the profunctor thing with co and contravariance at the same time: I don't think it helps understanding of variance.

It's written a bit more tutorial style than most of the tour, because this tend to be a point where many questions arise, and readers can use a gentle push on understanong.

Mostly a re-write.

* Begin with invariance, since it's the default without any annotations
* Move to covariance, which is the thing that's most intuitive for most people
* Introduce contravariance as the opposite of covariance
* Remove more contrived examples. Less is more
When you're trying to understand something, jokes are never funny
@SethTisue SethTisue self-assigned this Aug 10, 2022
Copy link
Member

@SethTisue SethTisue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

big improvement overall, but I have nitpicks


A type parameter `T` of a generic class can be made covariant by using the annotation `+T`. For some `class List[+T]`, making `T` covariant implies that for two types `A` and `B` where `B` is a subtype of `A`, then `List[B]` is a subtype of `List[A]`. This allows us to make very useful and intuitive subtyping relationships using generics.
By default, type paramters in scala are invariant: subtyping relationships between the type paramter aren't reflected in the parameterized type. To explore why this works the way it does, we look at a simple parameterized type, the mutable box.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/paramters/parameters/ (in more than one place)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/scala/Scala/

@SethTisue SethTisue merged commit e244cdd into scala:main Aug 26, 2022
@SethTisue
Copy link
Member

thank you Martijn!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants