docs.scala-lang
docs.scala-lang copied to clipboard
Typos (twice the same) in Tour of Scala : Variances
In section "Covariance":
We say that
ImmutableBoxis covariant inA, and this is indicated by the+before theA.More formally, that gives us the following relationship: given some
class Cov[+T], then ifAis a subtype ofB,Cov[A]is a subtype ofCov[B]. This allows us to make very useful and intuitive subtyping relationships using generics.
In section "Contravariance":
We say that
Serializeris contravariant inA, and this is indicated by the-before theA. A more general serializer is a subtype of a more specific serializer.More formally, that gives us the reverse relationship: given some
class Contra[-T], then ifAis a subtype ofB,Contra[B]is a subtype ofContra[A].
As stated in "Generic classes":
One convention is to use the letter A as type parameter identifier, though any parameter name may be used.
class Cov[+T] should be class Cov[+A].
class Contra[-T] should be class Contra[-A].
For context:
- Starting Scala
- Started Tour from beginning, never met generic defined with T letter
- In Java, convention is to use letter T instead of A as in Scala 3 (I think this is main reason for this issue.)
Not really sure with this. I can make a PR when confirmed.
Thanks
Hey, thank you for opening this issue. In that specific case, I would argue it is preferable not to use a type variable named A in the definition of Cov and Contra, to not confuse it with the type A mentioned afterward (“if A is a subtype of B”).
In the definition of Cov,
class Cov[+T]
T is a type parameter, which is later instantiated to A in the example:
Cov[A]
Here, A is not a type parameter, but a concrete type.
Maybe the confusion comes from the fact that we use A and B for concrete types although in the “Generic classes” pages we say that these names are conventionally used for type parameters.
So, I see the following possible ways forward:
- use names other than
AandBin the explanation. For instance, we could useT1andT2, orBaseandSub, or maybeXandY? - keep things as they are. Personally, I like the names
AandBhere, it makes the sentence “ifAis a subtype ofB, thenCov[A]is a subtype ofCov[B]” easier to read than “IfSubis a subtype ofBase, thenCov[Sub]is a subtype ofCov[Base]”, but that is probably highly subjective.