Version française
Home     About     Download     Resources     Contact us    

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

Browse thread
Why can't I call a function over a subclass?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-05 (15:00)
From: Luca de Alfaro <luca@d...>
Subject: Re: [Caml-list] Re: Why can't I call a function over a subclass?
The problem is also that the example I posted is a minimal instance of the
problem, but my real problem is much harder to solve (or so I believe, let
me know if you have suggestions!).
My real problem is that I have a class r, that internally, in many places,
uses objects of a class p.
Then I subclass r into r', and in r', I need to replace objects of type p
with objects of type p', a subclass of p.
Inside r I have a method do_work that does something like, in its body:

let x = new Pclass.p in

Now, if I cut-and-paste the code of do_work from r to r', and change the
above line in:
let x = new Pclass.p' in
then everything works.
If instead I replace the code in r with:

method make_p = new Pclass.p

let x = make_p in

so that in r', I just need to override make_p with:

method make_p = new Pclass.p'

then the code does not type check --- and I don't know why, and in any case
it is not logical to me.
What would be the solution in this case?  The classes p, p' are used in many
places in r, r', but the error only occurs when I do the above change.
Any help would be appreciated --- at least, so that I understand better how
not to break my code...


On 10/5/07, David Teller <> wrote:
> Sure. I'm just saying that this default behaviour is disconcerting.
> In addition to which, while it does appear in the documentation, it's
> rather well hidden inside section 3.11 "Polymorphic methods" -- even
> though this is not a method.
> Cheers,
> David
> On Fri, 2007-10-05 at 12:53 +0200, Zheng Li wrote:
> > David Teller <> writes:
> > > I agree with Luca that it's disconcerting.
> > > When r is a class, I would expect
> >
> > >  let f (x:r) (y:r) =...
> > This expression says f accept arguments of type [r], what you mean to
> say is: f
> > accept any arguments belong to [r]'s subtypes family. Then say it with
> >
> > # let f (x:#r) (y:#r) = x#get_x = y#get_x
> > val f : #r -> #r -> bool = <fun>
> >
> _______________________________________________
> Caml-list mailing list. Subscription management:
> Archives:
> Beginner's list:
> Bug reports: