Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Pietro Abate <Pietro.Abate@a...>
Subject: Re: [Caml-list] Why can't I call a function over a subclass?
On Fri, Oct 05, 2007 at 12:48:54AM -0700, Luca de Alfaro wrote:
> r' is a subclass of r.  The compiler complains:
> 
> File "class1.ml", line 12, characters 28-30:
> This expression has type r' but is here used with type r
> The second object type has no method get_xx

I see two solutions: Either you explicitly coerce the type of r' to r
so to match its (explicit) signature:
let q (r1: r') (r2: r') = f (r1 :> r) (r2 :> r) 

or you remove the type constraints from f and you let ocaml do the
rest.

let f r1  r2 = (r1#get_x = r2#get_x)
let q (r1: r') (r2: r') = f r1 r2

why do you want to specify the type of f ? The minimal information
needed is that it's an object with a method get_x and this can be
inferred by the type checker. Then all objects with a method get_x will
work even if not explicitly a subclass (iirc). Usually I don't specify
type constraints if not absolutely necessary and I use a signature to
wrap the module in the end.

:)
pp

-- 
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.gnu.org/philosophy/no-word-attachments.html