-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Variance rewrite #2488
Conversation
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
There was a problem hiding this 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
_tour/variances.md
Outdated
|
||
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. |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/scala/Scala/
thank you Martijn! |
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.