Covariance とは?
covariance は、型引数どうしの subtyping が、それらから構築されるジェネリック型に同じ方向で流れる variance で、ある subtype のコンテナは supertype のコンテナの subtype になります。読み取り専用のシーケンスのように、型パラメータが生成されるだけの場合に安全です。supertype が期待される場所で subtype を読み取ることは常に有効です。
なぜ重要か
covariance により、より一般的な型のリストが期待される場所に、より具体的な型のリストを読み取り専用で渡せます。いつ安全かを知ることで、過度に硬直した API と不健全な代入の両方を防げます。
関連ガイド
What Is Contravariance?Contravariance reverses subtyping direction, so a consumer of a supertype can stand in where a consumer of a…
What Is Variance in Type Systems?Variance describes how subtyping between type arguments carries over to subtyping between the generic types b…
What Is Bounded Quantification?Bounded quantification constrains a generic type parameter to be a subtype of some bound, so code can rely on…