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

Smells like duck-typing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2007-10-18 (16:39) From: skaller Subject: Re: [Caml-list] Smells like duck-typing
```
On Thu, 2007-10-18 at 17:02 +0200, Arnaud Spiwack wrote:
> >
> > Saying that A is a supertype of B is the equivelent of saying B is a
> > subtype of A.  Same relation, different direction.  In OO lingo, how
> > they say "B is a subtype of A" is that "B inherits from (is a subclass
> > of) A".
> That's not true. That's how you say it in *some* Object Oriented
> languages. Inheritance is a tool which allows you to derive another
> object from another one. You like it to be a subtype though, it usually
> is rather difficult to build a sound inheritance system where it can
> break subtyping anyway

That isn't so, almost ALL derived types fail to be subtypes.
In particular, this is always the case when there is a mutator,
because derived values are covariant but mutators are contravariant,
on the other hand Object Orientation intrinsically deals with
mutable state .. QED.

This combination destroys the OO paradigm instantly,
[quite apart from the usual linearity constraint which
make it impossible for OO to handle any kind of relationship]

To understand this simply consider a matrix (base type)
and a symmetric matrix (derived type). Every symmetric
matrix 'is-a' matrix. Fine. It works as a value.

But add a mutator, 'setxy(x,y)' with property that only element x,y
is modified and you're screwed: derived class mutators
work on the base, but not the other way around.

The soundness theory is simple: methods have to be covariant,
but mutators have to be contravariant, and since mutators
are also method they have to be *invariant*, and invariance
effectively means an OO  class cannot have subtypes.

The effect is, given some class type T, the ONLY thing
you are allowed to do with inheritance is implement it,
you cannot actually derive a new type.

[Now someone who understands the theory better than me
can re-explain this ..:]

--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

```