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 TAA03599 for caml-redistribution; Mon, 12 Apr 1999 19:32:36 +0200 (MET DST) 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 NAA22250 for ; Mon, 12 Apr 1999 13:34:14 +0200 (MET DST) Received: from morgon.inria.fr (morgon.inria.fr [128.93.8.33]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id NAA01811; Mon, 12 Apr 1999 13:34:13 +0200 (MET DST) Received: (from remy@localhost) by morgon.inria.fr (8.8.7/8.8.7) id MAA02666; Mon, 12 Apr 1999 12:39:05 +0200 Message-ID: <19990412123905.09954@morgon.inria.fr> Date: Mon, 12 Apr 1999 12:39:05 +0200 From: Didier Remy To: Markus Mottl Cc: OCAML Subject: Re: creating fresh objects of type 'self Reply-To: Didier.Remy@inria.fr References: <19990412103328.05240@morgon.inria.fr> <199904121103.NAA00431@miss.wu-wien.ac.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.85e In-Reply-To: <199904121103.NAA00431@miss.wu-wien.ac.at>; from Markus Mottl on Mon, Apr 12, 1999 at 01:03:54PM +0100 Organization: INRIA, BP 105, F-78153 Le Chesnay Cedex Phone: (33) 1 3963 5317 -- Sec: (33) 1 3963 5570 -- Fax: (33) 1 3963 5684 Web: http://cristal.inria.fr/~remy Sender: weis > This works, of course: > > class parent = object (self : 'self) > val mutable children : 'self list = [] > method add_fresh_object = children <- {<>} :: children > end > > But this is not the intended result: now we have added a *copy* of > the current object, not of its "fresh" state, i.e. the state it was in > immediately after creation. You could use an initializer to remember the "fresh" state in an instance variable (using {< >}), and use a copy of that instance variable in the method add_fresh_object. class parent = object (self : 'self) val mutable fresh = None val mutable children : 'self list = [] method add_fresh_object = let Some x = fresh in children <- Oo.copy x :: children initializer fresh <- Some {< >} end;; Anyway, you have apparently found your own solution. Best regards, Didier