Covariance and Contravariance (computer Science) - Avoiding The Need For Covariant Argument Types

Avoiding The Need For Covariant Argument Types

The need for covariant argument types arises from strategies in object oriented languages for context-sensitive selection of the code used in method calls. This does not include the first parameter of a method call, which is the object itself, which is not contravariant.

Castagna showed that types used for runtime selection of the method are covariant, while types not used for runtime selection of the method are contravariant. In Castagna's work, examples which would suggest the usage of covariance for parameter types are treated with the usage of multiple dispatch, i.e. overriding where the right method is selected also based on the type of some arguments; applying the rule, covariance is allowed for those argument types. However, this solution cannot be applied to most programming languages, since they do not support multiple dispatch.

Note that for (static) overload resolution, the opposite rule applies: types used for compile-time method selection (i.e. parameter types) are contravariant; types not used to select the method are covariant.

These terms are also used in the context of modern programming languages that offer other functors to create new types with type variables, e.g., generic programming or parametric polymorphism, and exception handling where method definitions are enriched with annotations that indicate possible failures.

Read more about this topic:  Covariance And Contravariance (computer Science)

Famous quotes containing the words avoiding the, avoiding, argument and/or types:

    The only sure way of avoiding these evils [vanity and boasting] is never to speak of yourself at all. But when, historically, you are obliged to mention yourself, take care not to drop one single word that can directly or indirectly be construed as fishing for applause.
    Philip Dormer Stanhope, 4th Earl Chesterfield (1694–1773)

    Children must eventually train their own children, and any impoverishment of their impulse life, for the sake of avoiding friction, must be considered a possible liability affecting more than one lifetime
    Erik H. Erikson (20th century)

    If we could produce one or two more Madame Curies, that would accomplish far more for the advancement of women than any amount of agitation, argument and legislation.
    Virginia Crocheron Gildersleeve (1877–1965)

    If there is nothing new on the earth, still the traveler always has a resource in the skies. They are constantly turning a new page to view. The wind sets the types on this blue ground, and the inquiring may always read a new truth there.
    Henry David Thoreau (1817–1862)