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
[Caml-list] does class polymorphism need to be so complicated?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-08-22 (04:00)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] does class polymorphism need to be so complicated?
From: Benjamin Geer <>

> Benjamin Geer wrote:
> > I'm still curious to know why the example I gave (returning a 
> > mysql_connection from a method that was typed to return a #connection) 
> > didn't compile, though ("This method has type string -> mysql_connection 
> > which is less general than 'a. string -> (#connection as 'a)").

Because #connection is _not_ an interface. It is just a polymorphic
type including all types having more methods than connection.
So returning a #connection means that the returned object already has
all the methods in the world, with all types imaginable. Nonsense.

#connection only makes sense on the left-hand side of an arrow.
By the way, the typing of #connection really uses rows (as by Remy and
later Pottier), but in a simplified way. So the unification Jacques
Carrette is talking about is already there from the beginning of ocaml.

> Curiously, it works if the class is parameterised instead of the method:
> class type ['a] driver =
> object
>    constraint 'a = #connection
>    method get_connection : db_name:string -> 'a
> end
> class mysql_driver =
> object (self : 's)
>    constraint 's = #connection #driver
>    method get_connection ~(db_name:string) =
>      new mysql_connection db_name
> end

The meaning is completely different. Your class type says that
get_connection must at least have all methods of connection.
But in mysql_driver, you end up with a get_connection returning a
(monomorphic) mysql_connection. No progress whatsoever.

Jacques Garrigue

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: