From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA02713; Sat, 17 Nov 2001 15:18:37 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id PAA02697 for ; Sat, 17 Nov 2001 15:18:36 +0100 (MET) Received: from staff.cs.usyd.edu.au (staff.cs.usyd.edu.au [129.78.8.1]) by nez-perce.inria.fr (8.11.1/8.10.0) with SMTP id fAHEIW920487 for ; Sat, 17 Nov 2001 15:18:34 +0100 (MET) Received: from hons.cs.usyd.edu.au. by staff.cs.usyd.edu.au.; Sun, 18 Nov 2001 01:18:22 +1100 Received: from localhost (mrak@localhost) by hons.cs.usyd.edu.au (8.9.3/8.9.3) with ESMTP id BAA28603 for ; Sun, 18 Nov 2001 01:18:22 +1100 X-Authentication-Warning: hons.cs.usyd.edu.au: mrak owned process doing -bs Date: Sun, 18 Nov 2001 01:18:22 +1100 (EST) From: Mark Wotton cc: caml-list@inria.fr Subject: Re: [Caml-list] [Q]: Co(ntra)variance and subtyping? In-Reply-To: <20011116203745.A59514@qiao.in-berlin.de> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Fri, 16 Nov 2001, Clemens Hintze wrote: > Two things I would like to ask you right now: > > - What does subtyping exactly mean in OCaml resp. functional > programming? > - What means covariance and contravariance of types and subtypes? Not being an Ocaml guru, I shan't attempt the first question. The second's pretty easy, though: covariance is the sane way of doing OO, and contravariance is bizarre. (Right, if there's any Eiffel devotees on here, I've just started a flamewar. :) Seriously: contravariance breaks the assumption that a subtype is capable of everything that its parent can do. Essentially, it allows you to narrow arguments to functions: to use some weird eiffelish pseudocode i had lying around, you can do this. class bar class bar_child is bar class garment public foo(b: bar ) class toga is garment public foo(b: bar_child) /* narrowing the argument of foo */ class tester public main = let p:garment = toga{} in p.foo(bar{}) /* attempting to call foo method of toga object using object of type bar: fails at runtime */ Basically, you expect to be able to call "foo" on any garment, and the only requirement is that the argument be of type "bar". "toga" narrows it to "bar_child" and the whole thing falls down in a heap. Covariance doesn't let this happen. You can actually widen the type in a child class, I believe: this isn't generally particularly useful, though. > Regards, > Clemens. > > PS: Sorry, if my english is not flawless, but it is not my > mother-tongue, and I do not speak french a bit, unfortunately. It's an odd thing: most of the posts with apologies for bad English I've seen on Usenet have been flawless as far as composition goes. 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