Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

HHVM Blog:
Covariance, Contravariance, and super Type Constraints
May 29, 2015 @ 15:13:24

The HHVM blog has a new post that talks about covariance, contravariance, and super type constraints - enhancements to the previous generics handling in the Hack language.

Hack has recently enhanced its generics with two features: variance annotations and super type constraints. In this post, I’ll explain how they work and why they were added.

They start with variance and how the idea of covariance (consistent type variance in class parameters) fits in. They include a code example showing how this typing works and some of the issues with following this covariance flow. Following this they talk about contravariance, the opposite of covariance, where the typing can be used as a parameter type but not a return type. They go on to talk about the idea of "super type constraints" and how they augment the current type constraint handling to provide improved type resolution. They end the post with a bit about how "super" relates to "as" constraints and a historical note about some hard-coded class names that are always resolved as either co- or contravariant by the typechecker.

tagged: covariance contravariance super typeconstraints example history

Link: http://hhvm.com/blog/9215/covariance-contravariance-and-super-type-constraints


Trending Topics: