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.<br>
<br>For a moment I thought you might be able to do something like this:<br><br>A cloneA;<br>class A {<br>        cloneA.x =&gt; int x;<br>        cloneA.y =&gt; int y;<br>}<br><br>1 =&gt; cloneA.x;<br>A a;<br>&lt;&lt;&lt; &quot;A.x=&quot;, a.x &gt;&gt;&gt;;<br>
<br>... which is kind of insane (what would clone.x be before you assign the 1!) Just for kicks I tried running it, rendering a &quot;NullPointerException&quot;, unsurprisingly.<br><br>/Stefan<br><br><div class="gmail_quote">
On Fri, Dec 11, 2009 at 3:05 PM, Hans Aberg <span dir="ltr">&lt;<a href="mailto:haberg@math.su.se">haberg@math.su.se</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
ChucK does not have cloning for reference object, right? That is, the semantic equivalent of an this explicit copy-constructor pseudocode:<br>
  class A {...}<br>
  A a0;<br>
  ...<br>
  new A(a0) @=&gt; A a1;<br>
That is, instead of merely setting a1 to the same reference as that of a0, create a new independent copy, and set its references to that of a0 (shallow copy).<br>
<br>
It is a problem in the program I wrote, <a href="http://scale.ck" target="_blank">scale.ck</a>: if modifying tuning data, one actually manipulates the data of the original tuning loaded into memory. This is cool, but if I want to be able to reload the original data, I will have to write my own cloning operator.<br>

<br>
The difference is illustrated in the code below. First I make a shallow copy of a to b, which means that I can change b without changing a. Then I set a reference of a to b, after which changing b also changes a.<br>
<br>
  Hans<br>
<br>
<br>
---- <a href="http://clone.ck" target="_blank">clone.ck</a> ----<br>
class A {<br>
  string s;<br>
}<br>
<br>
A a; &quot;a&quot; @=&gt; a.s;<br>
A b; &quot;b&quot; @=&gt; b.s;<br>
<br>
chout &lt;= &quot;a.s = &quot; &lt;= a.s &lt;= &quot;, b.s = &quot; &lt;= b.s &lt;= &quot;.\n&quot;;<br>
<br>
&quot;c&quot; @=&gt; b.s;<br>
chout &lt;= &quot;\&quot;c\&quot; @=&gt; b.s; a = &quot; &lt;= a.s &lt;= &quot;, b = &quot; &lt;= b.s &lt;= &quot;.\n&quot;;<br>
<br>
a.s @=&gt; b.s;<br>
chout &lt;= &quot;a.s @=&gt; b.s; a.s = &quot; &lt;= a.s &lt;= &quot;, b.s = &quot; &lt;= b.s &lt;= &quot;.\n&quot;;<br>
<br>
&quot;d&quot; @=&gt; b.s;<br>
chout &lt;= &quot;\&quot;d\&quot; @=&gt; b.s; a.s = &quot; &lt;= a.s &lt;= &quot;, b.s = &quot; &lt;= b.s &lt;= &quot;.\n&quot;;<br>
<br>
a @=&gt; b;<br>
chout &lt;= &quot;a @=&gt; b; a.s = &quot; &lt;= a.s &lt;= &quot;, b.s = &quot; &lt;= b.s &lt;= &quot;.\n&quot;;<br>
<br>
&quot;e&quot; @=&gt; b.s;<br>
chout &lt;= &quot;\&quot;e\&quot; @=&gt; b.s; a.s = &quot; &lt;= a.s &lt;= &quot;, b.s = &quot; &lt;= b.s &lt;= &quot;.\n&quot;;<br>
<br>
---- run ----<br>
$ chuck <a href="http://clone.ck" target="_blank">clone.ck</a><br>
a.s = a, b.s = b.<br>
&quot;c&quot; @=&gt; b.s; a = a, b = c.<br>
a.s @=&gt; b.s; a.s = a, b.s = a.<br>
&quot;d&quot; @=&gt; b.s; a.s = a, b.s = d.<br>
a @=&gt; b; a.s = a, b.s = a.<br>
&quot;e&quot; @=&gt; b.s; a.s = e, b.s = e.<br>
--------<br>
<br>
<br>
_______________________________________________<br>
chuck-users mailing list<br>
<a href="mailto:chuck-users@lists.cs.princeton.edu" target="_blank">chuck-users@lists.cs.princeton.edu</a><br>
<a href="https://lists.cs.princeton.edu/mailman/listinfo/chuck-users" target="_blank">https://lists.cs.princeton.edu/mailman/listinfo/chuck-users</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Release me, insect, or I will destroy the Cosmos!<br>