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 PAA26589 for caml-redistribution; Mon, 8 Feb 1999 15:18:31 +0100 (MET) Received: (from vouillon@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA20963; Mon, 8 Feb 1999 13:21:19 +0100 (MET) Message-ID: <19990208132119.65045@pauillac.inria.fr> Date: Mon, 8 Feb 1999 13:21:19 +0100 From: Jerome Vouillon To: ??????? ??????? , Caml list Subject: Re: self in classes References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Mailer: Mutt 0.89.1 In-Reply-To: ; from ??????? ??????? on Sat, Feb 06, 1999 at 05:09:54PM +0300 Sender: weis On Sat, Feb 06, 1999 at 05:09:54PM +0300, ??????? ??????? wrote: > Are there any ways to call the methods of other objects with self as an > argument ? [...] > When i'm trying to write something like that ocamlc says me that > self canot escape of it's definition If I understand right, you're trying to mutally define two classes this way: class foo = object (self) val observers : bar list = [] method register = List.iter (function o -> o#register self) observers end and bar = object method register _ = () end;; This fails, because the inferred type for the method register of class bar would be 'a -> unit, where 'a is the type of self in class foo. But this type should not appear outside class foo. There are two ways to solve the problem. The first is to let the method register of class bar have a more general type, and break the recursion between the two classes : class ['a] bar = object method register (_ : 'a) = () end;; class foo = object (self) val observers : 'a bar list = [] method register = List.iter (function o -> o#register self ) observers end;; The second way is to add a coercion of self to some fixed type: class foo = object (self) val observers : bar list = [] method register = List.iter (function o -> o#register (self :> < >) ) observers end and bar = object method register _ = () end;; -- Jérôme