Date: Mon, 12 Apr 1999 12:39:05 +0200
From: Didier Remy <Didier.Remy@inria.fr>
To: Markus Mottl <mottl@miss.wu-wien.ac.at>
Subject: Re: creating fresh objects of type 'self
In-Reply-To: <199904121103.NAA00431@miss.wu-wien.ac.at>; from Markus Mottl on Mon, Apr 12, 1999 at 01:03:54PM +0100
> 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
This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:22 MET