* creating fresh objects of type 'self
@ 1999-04-09 22:56 Markus Mottl
1999-04-12 8:33 ` Didier Remy
0 siblings, 1 reply; 5+ messages in thread
From: Markus Mottl @ 1999-04-09 22:56 UTC (permalink / raw)
To: OCAML
Hello,
is there a convenient way of creating objects of type 'self in classes?
Cloning is easy, e.g:
class foo = object
method clone = {< >}
end
which yields an object not necessarily of type "foo" but possibly of
a subclass.
But I wonder, how I can do something similar to get a "fresh" object.
I would like to be able to have something like:
class foo = object (_ : 'self)
method create = new 'self
end
The only possibility I know to get the intended result is rather
inconvenient: I do as in "clone", but reinitialize all member data with
their initial data. This is not only error-prone, but possibly quite a
lot of work.
Am I overseeing an obvious short solution?
The original idea is to have a parent *object* passing "self" to a child
*object* it has just created itself (note that I mean concrete objects,
not classes).
Best regards,
Markus Mottl
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: creating fresh objects of type 'self
1999-04-09 22:56 creating fresh objects of type 'self Markus Mottl
@ 1999-04-12 8:33 ` Didier Remy
1999-04-12 12:03 ` Markus Mottl
0 siblings, 1 reply; 5+ messages in thread
From: Didier Remy @ 1999-04-12 8:33 UTC (permalink / raw)
To: Markus Mottl; +Cc: OCAML
> is there a convenient way of creating objects of type 'self in classes?
>
> Cloning is easy, e.g:
>
> class foo = object
> method clone = {< >}
> end
>
> which yields an object not necessarily of type "foo" but possibly of
> a subclass.
Yes.
> But I wonder, how I can do something similar to get a "fresh" object.
The method clone already gives you a fresh copy of the original object. So
why aren't you happy with the method clone?
> I would like to be able to have something like:
>
> class foo = object (_ : 'self)
> method create = new 'self
> end
>
> The only possibility I know to get the intended result is rather
> inconvenient: I do as in "clone", but reinitialize all member data with
> their initial data. This is not only error-prone, but possibly quite a
> lot of work.
I don't understand what you mean here.
> The original idea is to have a parent *object* passing "self" to a child
> *object* it has just created itself (note that I mean concrete objects,
> not classes).
I still don't undertand what you really want to do.
Best regards,
Didier.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: creating fresh objects of type 'self
1999-04-12 12:03 ` Markus Mottl
@ 1999-04-12 10:39 ` Didier Remy
1999-04-12 13:10 ` Markus Mottl
0 siblings, 1 reply; 5+ messages in thread
From: Didier Remy @ 1999-04-12 10:39 UTC (permalink / raw)
To: Markus Mottl; +Cc: OCAML
> 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: creating fresh objects of type 'self
1999-04-12 8:33 ` Didier Remy
@ 1999-04-12 12:03 ` Markus Mottl
1999-04-12 10:39 ` Didier Remy
0 siblings, 1 reply; 5+ messages in thread
From: Markus Mottl @ 1999-04-12 12:03 UTC (permalink / raw)
To: Didier.Remy; +Cc: OCAML
> > But I wonder, how I can do something similar to get a "fresh" object.
>
> The method clone already gives you a fresh copy of the original object. So
> why aren't you happy with the method clone?
My initial idea was the following:
A parent object that has a list of child objects of the same type and
a method that adds a "fresh" (not a copy!) of the initial object (= as
the parent was created) to this list.
This following example does not work, of course, because "parent" is
not necessarily of type "'self".
class parent = object (self : 'self)
val mutable children : 'self list = []
method add_fresh_object = children <- new parent :: children
end
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.
At first I thought this could be solved with something like "new 'self",
but didn't think about the problem of what to do with objects that are
created with parameters: the existence of a function like "new 'self"
would then require that a reference to the parameters is kept for every
object so that it can be "freshly" created at anytime.
But the meaning of "new 'self" could be ambiguous if the parameters can
be changed with side-effects. Making copies of the initial parameters
to circumvent this problem is probably also not a good idea - this could
take up tons of memory.
Thus, I fear that a feature like "new 'self" is not one you might want
to add...
I have "solved" my problem now by using a closed object type for the
elements of the list "children". If I want to add an object, which is
of another type (maybe it's further down the inheritance hierarchie),
I just coerce (if possible) the object in question to this closed object
type. This solution is, unfortunately, not so flexible and elegant.
I hope my new problem description is a bit clearer than my last one...
Best regards,
Markus Mottl
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: creating fresh objects of type 'self
1999-04-12 10:39 ` Didier Remy
@ 1999-04-12 13:10 ` Markus Mottl
0 siblings, 0 replies; 5+ messages in thread
From: Markus Mottl @ 1999-04-12 13:10 UTC (permalink / raw)
To: Didier.Remy; +Cc: OCAML
> 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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~1999-04-12 17:32 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-04-09 22:56 creating fresh objects of type 'self Markus Mottl
1999-04-12 8:33 ` Didier Remy
1999-04-12 12:03 ` Markus Mottl
1999-04-12 10:39 ` Didier Remy
1999-04-12 13:10 ` Markus Mottl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox