Different programming languages support variance (covariance/contravariance) in different ways. My goal is to compare all supported types of variance in C#, Java and Scala, and to reason about why some architectural decisions have been made by language designers.
C#
|
Java
|
Scala
| |
Arrays covariance | +
(unsafe at runtime)
| +
(unsafe at runtime)
| _
(arrays are invariant by design)
Though, there is support for Java's "covariant" arrays, of course.
|
Arrays contravariance | _ | _ | _ |
Generics variance
(covariance/contravariance)
| +
Defined by a generic type creator (definition-site).
(Restricted to generic interfaces and generic delegates)
| +
Defined by clients of generic type using wildcards (use-site).
| +
Defined by a generic type creator (definition-site).
Also, there are existential types that cover Java's wildcards functionality.
|
Overriding: return type covariance | _ | + | + |
Overriding: parameter type contravariance | _ | _ | _ |
No comments:
Post a Comment