Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: subtyping and inheritance
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Didier Remy <Didier.Remy@i...>
Subject: Re: subtyping and inheritance
> I completely agree, it is quite difficult (maybe impossible) to do it if you
> want it fully general. 

Beppe, 

You won't be surprised if I tell you that I don't like special cases a lot.
Moreover, I don't really know where and how to draw a clear border. Even on
the particular example of money that you took, things are not so simple....

----------------

Typechecking of classes in Ocaml is (relatively) simple because it relies
only on polymorphism and not on subtyping. Subtyping comes later and it is
not essential. In particular, if you never use explicit coercions, there is
actually no subtyping involved at all, and most examples still typecheck,
including binary methods (and much more)!  As a result of this simple
approach, the type of self in a subclass is just an instance of the type of
self in its parent class.

I suspect that you will have to give up this invariant since your approach
is, conversely, based on subtyping and not on polymorphism.  Then, you may
be loosing much of the simplicity or just be unable to formalize the
``patch'' as an ``extension'' of Ocaml.

> # class money2 x =
>     object 
>       inherit money x
>        method dynamic leq (p:'mytype) = repr <= (p#value -. p#square)
>        method square  = repr *. repr
>     end;;

[the type annotation on "p" is useless you bound "'mytype" as the type of
self in "object (_ : 'mytype)"]

What type do you wish to give to that class? In particular, what should be
the (static) type of an object of that class?

> What the "dynamic" modifier means? First of all, it means that money2 is a
> subtype of money (that is it allows contravariant occurrences of
> 'mytype). 

Another essential matter is that type comparisons in Ocaml are structural,
as opposed to by-name: this applies to both type equality and
subtyping. Thus, you cannot just ``make'' money2 a subtype of money. Then,
how would you define the subtyping relation so that the type money2 is a
subtype of money?

In conclusion, I think that even when restricting to a small subcase of the
general problem the solution, if it exists as an extension of Ocaml, is not
easy. In any case, I would be very happy to see it on paper...

Cheers,

        -Didier