This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] [Q]: Co(ntra)variance and subtyping?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2001-11-17 (23:18) From: Mark Wotton Subject: Re: [Caml-list] Re: [Q]: Co(ntra)variance and subtyping?
```On Sat, 17 Nov 2001, Clemens Hintze wrote:

> - Covariance means that a specialized class also specialize the
>   'specializing' ...
> - Contravariance, however, is generalizing arguments of overloaded
>   methods in specialized classes.
>
> So in this light, your example should demonstrate covariance, isn't
> it? But what I do not like in the explanation above, is that it also
> only refer to OOP and not types.

You're quite right. I had it the wrong way around. Too little sleep...

> Another explanation I've found, was (a --> b means a subtypes b):
>
> - Covariance means, that if t' --> t ==> t' list --> t list

With this relationship, you would expect that "t' list" would be able to
do anything that "t list" could do, as it's a subtype: therefore, you'd
expect to be able to add a t to the front. With covariance, you
can't: it's expecting the more specialised type t'.

> - Contravariance, however, if t' --> t ==> t list --> t' list

This way, you can take "t list" and add either "t" or "t'" to the
front. Therefore, "t list" can do anything that "t' list" can, and the
intuitive notion of subtypes is preserved.

Apologies for getting it wrong the first time. It's doubly embarrassing as
it seems I made the same mistake in the thesis I just handed in...

mrak

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr

```