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 UAA03036; Mon, 29 Oct 2001 20:32:26 +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 UAA03344 for ; Mon, 29 Oct 2001 20:32:24 +0100 (MET) Received: from cmailg1.svr.pol.co.uk (cmailg1.svr.pol.co.uk [195.92.195.171]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f9TJWIn24213 for ; Mon, 29 Oct 2001 20:32:19 +0100 (MET) Received: from modem-32.duckdive.dialup.pol.co.uk ([62.25.152.32] helo=alba.sw) by cmailg1.svr.pol.co.uk with esmtp (Exim 3.13 #0) id 15yI8v-0007Z6-00 for caml-list@inria.fr; Mon, 29 Oct 2001 19:32:17 +0000 Received: by alba.sw (Postfix, from userid 1001) id 88169F8; Mon, 29 Oct 2001 20:30:36 +0000 (GMT) Date: Mon, 29 Oct 2001 20:30:36 +0000 From: Andrew Lawson To: caml-list@inria.fr Subject: Re: [Caml-list] Passing self to a new object Message-ID: <20011029203036.A1039@alba.sw> Reply-To: Andrew Lawson References: <20011028155625.A1303@alba.sw> <20011029180207D.garrigue@kurims.kyoto-u.ac.jp> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20011029180207D.garrigue@kurims.kyoto-u.ac.jp>; from garrigue@kurims.kyoto-u.ac.jp on Mon, Oct 29, 2001 at 06:02:07PM +0900 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Mon, Oct 29, 2001 at 06:02:07PM +0900, Jacques Garrigue wrote: > 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. Ouch, I knew that even such an otherwise nice language would turn out to have some fuzzy bits someday :) > (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.) I admit this was a quick example of the problem, not the program itself, which has many more hairy bits. > The general solution to this is to first define a class type: > class type xyz_t = object ('self) > method > btnNew : ... > ... > end Well this gives me an excuse to start playing with types and interfaces anyway. Is this a 'known problem, will be sorted eventually' or a 'just live with it' sort of thing? > Then change the code in btnNew to > new abc (self :> xyz_t) Casting! and here was I giving a C++ programmer some abuse about this last week :) > 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. Well, thats not so bad then ... I suppose good style should ahve me doing this anyway. > Hope this helps, Most definitely > Jacques Garrigue Thanks very much Andrew -- Andrew Lawson andrew@absentis.com ------------------- 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