argument contravariance

T

Tony Johansson

Hello!!

Can somebody explain what does argument contravariance mean.
I know what covariance mean because this is involved in some way when we
have argument contravariance .

Use some easy code exampe to describe this.


//Tony
 
A

Alf P. Steinbach

* Tony Johansson:
Can somebody explain what does argument contravariance mean.
I know what covariance mean because this is involved in some way when we
have argument contravariance .

Use some easy code exampe to describe this.

That last sounds like HOMEWORK -- however...

Co-variant means that an argument type is more specialized in a more
specialized (a derived) class, that's what "co" -- with -- stands for.

I mention that, which you claim to know, because contra-variant is just the
opposite, and that shouldn't be too hard to guess... Or Google.

Co-variant is compatible with the Liskov Substitution Principle (LSP) for an
out-argument, and contra-variant is compatible with LSP for an in-argument.

C++ does not support designation of argument data flow direction except for
function return values, so C++ does not support co/contra-variant arguments
except for function return values, which are allowed to be [insert the right
term here, Tony, is it in or out?]. C# lets you specify that an argument is
an out-argument, but currently does not support [the right term] for arguments
designated as out. Ada allows you to designate arguments as in or out, but I
don't know what Ada supports regarding co- and contra-variance.
 
G

Greg

Alf said:
* Tony Johansson:

That last sounds like HOMEWORK -- however...

I think it's also quite likely that the word "contravariance" came up
in some casual conversation, and the original poster was simply too
embarrassed to ask what such a commonly-used word means. And the
request for code samples I'm sure has an equally innocent motivation. I
just haven't thought of one yet.
Co-variant means that an argument type is more specialized in a more
specialized (a derived) class, that's what "co" -- with -- stands for.

I mention that, which you claim to know, because contra-variant is just the
opposite, and that shouldn't be too hard to guess... Or Google.

Co-variant is compatible with the Liskov Substitution Principle (LSP) for an
out-argument, and contra-variant is compatible with LSP for an in-argument.

C++ does not support designation of argument data flow direction except for
function return values, so C++ does not support co/contra-variant arguments
except for function return values, which are allowed to be [insert the right
term here, Tony, is it in or out?]. C# lets you specify that an argument is
an out-argument, but currently does not support [the right term] for arguments
designated as out. Ada allows you to designate arguments as in or out, but I
don't know what Ada supports regarding co- and contra-variance.

One example of contravariance in C++ would be type of object needed to
resolve a member pointer. In order to resolve a member pointer it is
necessary either to have an instance of the member pointer's class, or
of a derived class. Therefore we can say the type required to resolve a
member pointer is contravariant to the member pointer's class type.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top