From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA10743 for caml-redistribution; Wed, 27 Jan 1999 19:28:39 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id PAA07126 for ; Wed, 27 Jan 1999 15:28:34 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id PAA23964; Wed, 27 Jan 1999 15:28:31 +0100 (MET) Received: (from vouillon@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA18067; Wed, 27 Jan 1999 15:28:31 +0100 (MET) Message-ID: <19990127152831.14918@pauillac.inria.fr> Date: Wed, 27 Jan 1999 15:28:31 +0100 From: Jerome Vouillon To: Hendrik Tews , caml-list@inria.fr Subject: Re: subtyping and inheritance References: <19990118205542.06048@pauillac.inria.fr> <199901182118.WAA09639@miss.wu-wien.ac.at> <199901201150.MAA09911@irritatie.cs.kun.nl> Mime-Version: 1.0 X-Mailer: Mutt 0.89.1 In-Reply-To: <199901201150.MAA09911@irritatie.cs.kun.nl>; from Hendrik Tews on Wed, Jan 20, 1999 at 12:50:21PM +0100 Sender: weis On Wed, Jan 20, 1999 at 12:50:21PM +0100, Hendrik Tews wrote: > There was already a proposal how to solve the problem by Jerome > Vouillon. Just for demonstration I append another solution, which > uses a self implemented type case. It supports code reuse better, > but loses type security by using the Obj module. Maybe one of the > people who "understand the whole source code for the O'Caml > compiler, runtime and libraries" and by that can use the Obj > module, can comment on its use here. Your example indeed works, as coercions do not actually modify objects. When you need to make use of the Obj module, it is however advised to limit this use to a small module with a type-safe interface. Your example can be rewritten this way: class top = object inherit Property.c method eq (x : top) = print_string "top = top ? "; true end;; let left_k = Property.create_key ();; class left (l1 : int) = object (self) inherit top as super initializer Property.add left_k self l1 method eq x = try let l1' = Property.find left_k x in print_string "left = left ? "; l1 = l1' with Not_found -> super#eq x end;; let right_k = Property.create_key ();; class right (r1 : int) = object (self) inherit top as super initializer Property.add right_k self r1 method eq x = try let r1' = Property.find right_k x in print_string "right = right ? "; r1 = r1' with Not_found -> super#eq x end;; -- Jérôme type 'a t type u and u' class virtual s : object method virtual properties : u method virtual add_property : u' -> unit end class c : object method properties : u method add_property : u' -> unit end val create_key : unit -> 'a t val add : 'a t -> #c -> 'a -> unit val find : 'a t -> #c -> 'a filename="property.ml" type 'a t = int module Properties = Map.Make (struct type t = int let compare = compare end) type u' = int * Obj.t type u = Obj.t Properties.t class virtual s = object method virtual properties : u method virtual add_property : u' -> unit end class c = object val mutable properties = (Properties.empty : u) method properties = properties method add_property (k, p) = properties <- Properties.add k p properties end let i = ref (-1) let create_key () = incr i; !i let add p x v = x#add_property (p, Obj.repr v) let find p x = Obj.obj (Properties.find p (x#properties))