From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA21356; Mon, 29 Oct 2001 10:03:05 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA21533 for ; Mon, 29 Oct 2001 10:02:55 +0100 (MET) Received: from tet.kurims.kyoto-u.ac.jp (L025105.ppp.dion.ne.jp [211.126.25.105]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f9T92mn14130 for ; Mon, 29 Oct 2001 10:02:53 +0100 (MET) Received: from localhost (localhost [127.0.0.1]) by tet.kurims.kyoto-u.ac.jp (8.11.6/8.11.6) with ESMTP id f9T928K01188; Mon, 29 Oct 2001 18:02:08 +0900 (JST) (envelope-from garrigue@kurims.kyoto-u.ac.jp) To: andrew@absentis.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] Passing self to a new object In-Reply-To: <20011028155625.A1303@alba.sw> References: <20011028155625.A1303@alba.sw> X-Mailer: Mew version 1.94.2 on Emacs 20.7 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20011029180207D.garrigue@kurims.kyoto-u.ac.jp> Date: Mon, 29 Oct 2001 18:02:07 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Andrew Lawson > I'm writing a gui program where the callback for a button > creates a new object (also a gui) and needs to pass it a reference to > itself in order that the new object can contact the original for > information. The classes look something like this; > > class xyz = > let top = ... in > let ... = ... in > object (self) > method btnNew = ... ~command:(fun () = new abc self) top in > ... > ... > end > > and abc (parent:xyz) = > object (self) > var myparent = parent > ... > end > >My error is; > ..... > Self type cannot escape its class You've bumped into a painful quirk of ocaml's object system: self's type is not xyz, and must be handled with care. (By the way, your program seemed strange, since you were using self and abc before defining them, but I suppose it was a copy error, and I corrected it.) The general solution to this is to first define a class type: class type xyz_t = object ('self) method btnNew : ... ... end Then change the code in btnNew to new abc (self :> xyz_t) Remark that making xyz_t exact may be a bit lengthy, but you may leave out methods you don't need to call from abc. Hope this helps, Jacques Garrigue ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr