Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Benjamin Geer <ben@s...>
Subject: Re: [Caml-list] does class polymorphism need to be so complicated?
Jacques Garrigue wrote:
> OK, there's also another way to do it, without inheritance. I just
> tried not to be confusing.
> 
> class type printer = object
>   method virtual print : #printable -> unit
> end
> 
> class my_printer () = object (self : #printer)
>    method print obj = ...
> end

I've just tried a little experiment with this, and it doesn't seem to 
work in the case I had in mind:

(* A simple API *)

class type connection =
   object
     method close : unit
   end ;;

class type driver =
   object
     method get_connection : string -> #connection
   end ;;

(* An implementation of the API *)

class mysql_connection db_name =
   object (self : 's)
     constraint 's = #connection	
     val _db_name = db_name
	
     method close =
       print_string "closing connection ";
       print_string _db_name;
       print_newline();

     (* An extra method, which could be used by the driver *)
     method get_status = "OK"
   end ;;

class mysql_driver =
   object (self : 's)
     constraint 's = #driver
     method get_connection db_name =
       new mysql_connection db_name
   end;;

Caml rejects the definition of 'get_connection' in 'mysql_driver':

This method has type string -> mysql_connection which is less general than
   'a. string -> (#connection as 'a)

Ben

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners