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)

    If men as individuals surrender to the call of their elementary instincts, avoiding pain and seeking satisfaction only for their own selves, the result for them all taken together must be a state of insecurity, of fear, and of promiscuous misery.
    Albert Einstein (1879–1955)

    Because a person is born the subject of a given state, you deny the sovereignty of the people? How about the child of Cuban slaves who is born a slave, is that an argument for slavery? The one is a fact as well as the other. Why then, if you use legal arguments in the one case, you don’t in the other?
    Franz Grillparzer (1791–1872)

    As for types like my own, obscurely motivated by the conviction that our existence was worthless if we didn’t make a turning point of it, we were assigned to the humanities, to poetry, philosophy, painting—the nursery games of humankind, which had to be left behind when the age of science began. The humanities would be called upon to choose a wallpaper for the crypt, as the end drew near.
    Saul Bellow (b. 1915)