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-21 (18:05)
From: brogoff@s...
Subject: Re: [Caml-list] does class polymorphism need to be so complicated?
On Thu, 21 Aug 2003, Benjamin Geer wrote:
> Alternatively, you could use a virtual base class 'connection', and 
> always downcast the implementing class before passing it to application 
> code.  But this places an additional burden on the library author.

I think the burden is very slight, but I have no problem at all with using 
functions outside of objects. Realistic implementations would provide 
coercion functions for every base class you want to coerce to, perhaps 
named something like "as_base_class_name". Using the exmaple you give in 
another message, we get something like this 

(* A simple API *)

class virtual connection =
     method virtual close : unit
   end ;;

class virtual driver =
     method virtual get_connection : string -> connection
   end ;;

let as_connection o = (o : #connection :> connection);;

(* An implementation of the API *)

class mysql_connection db_name =
    inherit connection

    val _db_name = db_name

    method close =
      print_string "closing connection ";
      print_string _db_name;

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

class mysql_driver =
     inherit driver
     method get_connection db_name =
       as_connection (new mysql_connection db_name)

which doesn't seem bad compared to your original desired code. 

-- Brian

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