The significance of contravariance and covariance

Source: Internet
Author: User

Contravariance and covariance exist in strongly typed languages, although they are seldom mentioned, but they contain a description of the problem. Thanks to the people I discussed with.

Here, the use of C #, Scala contains an inverse parameter declaration method.

First, the definition of the inverse change

The contravariant parameter can be substituted by the subtype of the specified type, and the inverse of Scala is declared: Function1[-a,+b]; The parameter can use a type A or a subtype of Class A.

Second, the use of covariance and contravariance is different

1. Semantics

A common place to use in the parameters passed in for a function, Function1[-a,+b]

The inverse of the input means "as long as this function can be satisfied", so the function of satisfying is in the subclass of a, and the inverse emphasizes the function-"What can be done".

By the way, the covariance, the output covariance, the covariant emphasis is the type, a type can replace the parent class, such as beef is meat. The covariant emphasis is "what".

2. Knives and meat

Type: Food <-meat <-beef. Weapon <-knife <-beef knife. (<-represents an inheritance relationship: Parent class <-child class)

Scenario: function Functionx requires a knife (emphasizing the function of the knife) that produces meat. Generally defined as functionx[a,b], ordinary knives (knives) will produce ordinary meat (meat), beef knives will produce beef.

Type of a? Type of B? How to determine

The type of a can be a knife and a beef knife, because the beef knife is also a knife. Even the sub-class of the knife can be satisfied, from the inheritance of the knife's subclasses are knives.

So the type of a should be contravariant ——-knives (knives and subclasses)

Because the meat is made, the B type certainly contains meat, but is not sure that it is beef. So we can set the return to type B.

For this scenario, our final definition of Functionx is: functionx[-knife, + meat]

What about co-change?

We don't see the covariance, actually in C # and Scala, we set a type of food to receive the return value of the Functionx without getting an error. Because all return types are declared covariant in the language, that is, the actual definition is functionx[-knife, + meat]. The reason for this is: if I return a meat, then this meat must be food, I can always use the return type of the parent type in place of the returned object.

Third, another example

Icompareable<in T> emphasizes the function of "comparable", which is the inverse change.

Ienumerable<out T> emphasizes the "number of" type, which is covariant. Take list<t> explain,list< meat > said "I put meat in the list", can say "I put food in the list" is correct, that can use list< food > instead. But can't say "I put beef in the list", so it's wrong to use list< beef > instead.

The significance of contravariance and covariance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.