Skip to content

Commit 17e595d

Browse files
oderskyKordyjan
authored andcommitted
Explain isDeclaredVarianceLambda better
What variances on a type lambda mean is a subtle point, which warrants some explanations. [Cherry-picked 6024ba3]
1 parent 721e7c8 commit 17e595d

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,28 @@ object Types {
444444
case _ => NoType
445445
}
446446

447-
/** Is this a higher-kinded type lambda with given parameter variances? */
447+
/** Is this a higher-kinded type lambda with given parameter variances?
448+
* These lambdas are used as the RHS of higher-kinded abstract types or
449+
* type aliases. The variance info is strictly needed only for abstract types.
450+
* For type aliases we allow the user to write the variance, and we use it
451+
* to check that the structural variance of the type lambda is compatible
452+
* with the declared variance, and that the declared variance is compatible
453+
* with any abstract types that are overridden.
454+
*
455+
* But it's important to note that the variance of a type parameter in
456+
* a type lambda is strictly determined by how it occurs in the body of
457+
* the lambda. Declared variances have no influence here. For instance
458+
* the following two lambdas are variant, even though no parameter variance
459+
* is indicated:
460+
*
461+
* [X] =>> List[X] // covariant
462+
* [X] =>> X => Unit // contravariant
463+
*
464+
* Why store declared variances in lambdas at all? It's because type symbols are just
465+
* normal symbols, and there is no field in a Symbol that keeps a list of variances.
466+
* Generally we have the design that we store all info that applies to some symbols
467+
* but not others in the symbol's types.
468+
*/
448469
def isDeclaredVarianceLambda: Boolean = false
449470

450471
/** Does this type contain wildcard types? */

0 commit comments

Comments
 (0)