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 TAA31898 for caml-redistribution; Mon, 12 Apr 1999 19:32:53 +0200 (MET DST) 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 OAA16855 for ; Mon, 12 Apr 1999 14:10:49 +0200 (MET DST) Received: from miss.wu-wien.ac.at (miss.wu-wien.ac.at [137.208.107.17]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id OAA18437; Mon, 12 Apr 1999 14:10:46 +0200 (MET DST) Received: (from mottl@localhost) by miss.wu-wien.ac.at (8.9.0/8.9.0) id OAA08612; Mon, 12 Apr 1999 14:10:45 +0200 (MET DST) From: Markus Mottl Message-Id: <199904121210.OAA08612@miss.wu-wien.ac.at> Subject: Re: creating fresh objects of type 'self To: Didier.Remy@inria.fr Date: Mon, 12 Apr 1999 14:10:45 +0100 (MET DST) Cc: caml-list@inria.fr (OCAML) In-Reply-To: <19990412123905.09954@morgon.inria.fr> from "Didier Remy" at Apr 12, 99 12:39:05 pm X-Mailer: ELM [version 2.4 PL21] MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: weis > 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. Ah! Yes! I hardly ever use initializers so this idea didn't come to me! I will try to rewrite my solution and see how this fits into it... By the way: the example should probably be rewritten as: class parent = object (self : 'self) val mutable fresh = None val mutable children : 'self list = [] method private make_fresh = fresh <- Some {< >} method add_fresh_object = let Some x = fresh in let new_object = Oo.copy x in new_object#make_fresh; children <- new_object :: children initializer self#make_fresh end This makes sure that the children, too, are able to add fresh objects to their children list (i.e. the grandchildren). Thanks for your hint! Best regards, Markus Mottl -- Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl