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 (10:54)
From: Andrej Bauer <Andrej.Bauer@f...>
Subject: Re: [Caml-list] Why can't I call a function over a subclass?
Ocaml will compute types for you. If you want, you can also specify the 
types and it will repsect that. You specified the types of r1 and r2:

> let f (r1: r) (r2: r) : bool = (r1#get_x = r2#get_x)

Even though r' is a subclass  of r, a value of type r' is _not_ of type 
r (if your brain thinks in Java you might find this surprising--in which 
case we can talk about it):

# let a = new r' 5 ;;
val a : r' = <obj>
# (a : r);;
Characters 1-2:
   (a : r);;
This expression has type r' but is here used with type r
Only the first object type has a method get_xx

If you let Ocaml compute types on its own, your example will work as 

let f r1 r2 = (r1#get_x = r2#get_x)

Now the function of f is computed to be

val f : < get_x : 'a; .. > -> < get_x : 'a; .. > -> bool

So just let the machine worry about they types.