2 Jun 1996 01:19
ocaml, inheritance vs. subtyping?
Ian T Zimmerman <itz <at> rahul.net>
1996-06-01 23:19:02 GMT
1996-06-01 23:19:02 GMT
Hi, I am just starting with ocaml, so please have mercy on a newbie(Continue reading)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,
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,
RSS Feed