Unraveling the fog of covariance and contravariance: Simplifying matrices
Even after consulting a lot of information, the understanding of covariance and contravariance, especially their relationship with "input/output", is still confusing. To demystify these concepts, let’s break down their definitions and applications step by step.
Covariance and contravariance
Covariance allows the use of "broader" (less specific) types in the API instead of primitive types that are used only as "outputs" (e.g. return values). Contravariance, on the other hand, allows a "narrower" type (more specific) to be used in place of a primitive type in an API that is only used as an "input" (for example, a parameter).
Relate covariance/contravariance to input/output
The difference between "input/output" roughly corresponds to contravariance and covariance respectively. However, this relationship depends on whether the generic type is used as a method parameter or a return value.
Input/output of method parameters
When a generic type is used as a method parameter, the "contravariance" rule applies. This means that a method can accept more specific type parameters than the specified generic type. For example:
<code>interface MyInterface<in T> { T MyFunction(T variable); }</code>
In this case, a parameter of type Derived can be passed even though MyFunction expects a parameter of type Base.
Input/output of return value
When a generic type is used as a method return value, the "covariance" rules apply. A method can return a type value that is more general than the specified generic type. For example:
<code>interface MyInterface<out T> { T MyFunction(int variable); }</code>
In this case, MyFunction can return a value of type Base even though it returns a value of type Derived.
Understand its principles
Covariance of return values ensures that methods returning more general types can be implicitly assigned to variables of the specified generic type. Contravariance of parameters guarantees that methods that accept more specific parameter types can still be called with parameters of the specified generic type.
Conclusion
Mastering covariance and contravariance requires understanding the subtleties of type compatibility. By understanding how these concepts relate to the input/output distinction, you can effectively leverage generics to increase the flexibility and security of your code.
The above is the detailed content of Covariance and Contravariance: How Do 'In' and 'Out' Types Affect Generic APIs?. For more information, please follow other related articles on the PHP Chinese website!