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: 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 =
   object
     method virtual close : unit
   end ;;

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

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

(* An implementation of the API *)

class mysql_connection db_name =
  object
    inherit 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
     inherit driver
     method get_connection db_name =
       as_connection (new mysql_connection db_name)
   end;;

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

-- Brian


-------------------
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