Skip to content

Commit 6024ba3

Browse files
committed
Explain isDeclaredVarianceLambda better
What variances on a type lambda mean is a subtle point, which warrants some explanations.
1 parent 51d69e8 commit 6024ba3

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
@@ -439,7 +439,28 @@ object Types {
439439
case _ => NoType
440440
}
441441

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

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

0 commit comments

Comments
 (0)