型システムにおける Variance とは?
variance は、ある subtype から構築されたジェネリック型が、同じジェネリックを supertype から構築したものの subtype になるかどうかを定める規則です。型パラメータの使われ方に応じて、covariance、contravariance、invariance の 3 種類があります。variance は、パラメータ化された型どうしのどの置き換えを type system が安全に許すかを決定します。
なぜ重要か
variance を正しく扱うことで、ジェネリックなコレクションや関数型を実行時の型エラーなしに安全に置き換えられます。ジェネリック型どうしの一見直感的な代入がなぜ拒否されるのかも説明できます。
関連ガイド
What Is Covariance?Covariance preserves subtyping direction, so a generic over a subtype is a subtype of the same generic over a…
What Is Contravariance?Contravariance reverses subtyping direction, so a consumer of a supertype can stand in where a consumer of a…
What Is Bounded Quantification?Bounded quantification constrains a generic type parameter to be a subtype of some bound, so code can rely on…