[chuck-users] Cloning

Hans Aberg haberg at math.su.se
Fri Dec 11 09:50:46 EST 2009


On 11 Dec 2009, at 15:42, Stefan Blixt wrote:

> Yeah, the trouble with making your own clone method here is that  
> members are initialized once, and then written to again in the clone  
> method. The cool thing about constructors is that it offers a way to  
> make a first initialization of members depending on input to the  
> constructor.
>
> For a moment I thought you might be able to do something like this:
>
> A cloneA;
> class A {
>         cloneA.x => int x;
>         cloneA.y => int y;
> }
>
> 1 => cloneA.x;
> A a;
> <<< "A.x=", a.x >>>;
>
> ... which is kind of insane (what would clone.x be before you assign  
> the 1!) Just for kicks I tried running it, rendering a  
> "NullPointerException", unsurprisingly.

I think just writing
   fun A clone(A x) {
     A tmp;
     ...    // Set data of x to tmp.
     return tmp;
   }
will do it.

   Hans





More information about the chuck-users mailing list