ocaml, inheritance vs. subtyping?

Ian T Zimmerman (itz@rahul.net)
Sat, 1 Jun 1996 16:19:02 -0700

Date: Sat, 1 Jun 1996 16:19:02 -0700
Message-Id: <199606012319.QAA00493@kronstadt.rahul.net>
From: Ian T Zimmerman <itz@rahul.net>
To: caml-list@pauillac.inria.fr
Subject: ocaml, inheritance vs. subtyping?

Hi, I am just starting with ocaml, so please have mercy on a newbie
:-)

I am somewhat perplexed by the passage in the manual on virtual
methods (pp. 29-30). Let's start with the first paragraph on p.30: it
says that int_comparable2 is not a subtype of int_comparable, even
though the former inherits from the latter! Now for a person with an
experience in a non-functional OO language (C++ or Eiffel or Sather)
it seems _very strange_ that the subtype and inheritance (derivation)
relations aren't the same. It even seems that not having such an
overlap defeats the purpose of having OO features in the first place.
How is the programmer to predict where a subtype relation actually
exists?

Now the manual goes on to say that this occurs because `the self type
appears in contravariant position in the type of method leq'. First,
am I right in decoding this as `the types of the argument of
int_comparable#leq and int_comparable2#leq are in covariant relation,
but they would have to be in contravariant relation for a subtyping
relation to exist between int_comparable and int_comparable2'?
Second, and this may be the heart of the issue, _WHY_ is type 'a ->
bool inferred for leq, and not < x: unit -> int; .. > -> bool , as
it surely would for a global function?

If these are stupid questions, I apologize again. Maybe it's a sign
that the OO features need more explanation in the manual, though.

Best,

-- 
Ian T Zimmerman           +-------------------------------------------+
Box 13445                 I When the dead are left to bury the dead,  I
Berkeley CA 94712 USA     I the living remain alone.  Arthur Koestler I
mailto:itz@rahul.net      +-------------------------------------------+