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:

    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)

    As you are entered with the class of Nat. philosophy, give to it the hours of lecture, but devote all your other time to Mathematics, avoiding company as the bane of all progress.
    Thomas Jefferson (1743–1826)

    There is no good in arguing with the inevitable. The only argument available with an east wind is to put on your overcoat.
    James Russell Lowell (1819–1891)

    The American man is a very simple and cheap mechanism. The American woman I find a complicated and expensive one. Contrasts of feminine types are possible. I am not absolutely sure that there is more than one American man.
    Henry Brooks Adams (1838–1918)