Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: creating fresh objects of type 'self
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <mottl@m...>
Subject: Re: creating fresh objects of type 'self
> 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