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)

    There is all the difference in the world between the criminal’s avoiding the public eye and the civil disobedient’s taking the law into his own hands in open defiance. This distinction between an open violation of the law, performed in public, and a clandestine one is so glaringly obvious that it can be neglected only by prejudice or ill will.
    Hannah Arendt (1906–1975)

    Mistakes are made on two counts: an argument is either based on error or incorrectly developed.
    Thomas Aquinas (c. 1225–1274)

    Our major universities are now stuck with an army of pedestrian, toadying careerists, Fifties types who wave around Sixties banners to conceal their record of ruthless, beaverlike tunneling to the top.
    Camille Paglia (b. 1947)