I think you can pretty much say that ChucK follows Java&#39;s attitude towards objects, where the declaration of any non-primitive always contains a pointer to the memory where the object&#39;s data is stored, as opposed to containing the acual values. That&#39;s why this doesn&#39;t compile:<div>
<br></div><div><div>class A {</div><div>        int x;</div><div>}</div><div><br></div><div>A a1;</div><div>1 =&gt; a1.x;</div><div>A a2;</div><div>a1 =&gt; a2;</div><div>2 =&gt; a2.x;</div><div>&lt;&lt;&lt; &quot;a1.x=&quot;, a1.x, &quot; a2.x=&quot;, a2.x&gt;&gt;&gt;;</div>
<div><br></div><div><div>Hailstone:~ stefanblixt$ chuck <a href="http://test.ck">test.ck</a></div><div>[<a href="http://test.ck">test.ck</a>]:line(8): cannot resolve operator &#39;=&gt;&#39; on types &#39;A&#39; and &#39;A&#39;...</div>
<div>[<a href="http://test.ck">test.ck</a>]:line(8): ...(note: use &#39;@=&gt;&#39; for object reference assignment)</div><div><br></div><div>After I&#39;ve put the @ in there as the error message suggests, I get this output from the program:</div>
<div><br></div><div>a1.x= 2  a2.x= 2 </div><div><br></div><div>To create copies of an object you need to actively make a method in that class that takes an instance as input, creates a completely new instance, copies each member and then returns the new instance. There is no copy constructor. See Java&#39;s <a href="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()">clone</a> method in java.lang.Object.</div>
<div><br></div><div>Comparing with C/C++, there you can declare a struct/class variable that isn&#39;t a pointer but rather contains all member data where it was declared, allowing you to do crazy stuff like this:</div><div>
<br></div><div><div>#include &lt;stdio.h&gt;</div><div><br></div><div>struct {</div><div>int x;</div><div>} a1;</div><div>struct {</div><div>int x;</div><div>} a2;</div><div><br></div><div>int main(char** argv) {</div><div>
        a1.x = 1;</div><div>        a2.x = 3;</div><div>        *(&amp;(a1.x) +4) = 2;</div><div>        // this may print &quot;a1.x=1 a2.x=2&quot; if the alignment is correct</div><div>        printf(&quot;a1.x=%d a2.x=%d\n&quot;, a1.x, a2.x); </div>
<div>}</div><div><br></div><div>The confusing bit in ChucK is that it allows declarations with or without a @ before the variable name. My interpretation of this is that</div><div><br></div><div>A a;</div><div><br></div><div>
should be seen as a kind of shorthand for this:</div><div><br></div><div>new A @=&gt; A @ a;</div><div><br></div><div>These two staments are identical, I think.</div><div><br></div><div>/Stefan</div></div><div><br></div><div>
 </div></div><br><div class="gmail_quote">On Sun, Oct 4, 2009 at 8:57 AM, 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="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
On 4 Oct 2009, at 02:21, Adam Tindale wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This is a more direct version of what I meant to say. The problem I<br>
encountered was that I was hoping to do some operations in a function<br>
and was hoping that I was passing a copy of an object, much like what<br>
happens with primitives. Since objects pass by reference then weird<br>
side effects and bugs can easily creep into a fellow ChucKist&#39;s code.<br>
</blockquote>
<br></div>
Yes, that is one side effect of it: side effects. The reference objects reference the same object on the heap, also when passed into a function.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Anyways, I hope this example is more clear.<br>
<br>
--art<br>
<br>
class test{<br>
   0 =&gt; int val;<br>
}<br>
<br>
test obj1;<br>
&lt;&lt;&lt; obj1.val , &quot;original object&quot;&gt;&gt;&gt;;<br>
<br>
fun void testclasspassing(test i)<br>
{<br>
   4 =&gt; i.val;<br>
   &lt;&lt;&lt; i.val, &quot;in function&quot; &gt;&gt;&gt;;<br>
   // side effect!<br>
}<br>
<br>
testclasspassing(obj1);<br>
&lt;&lt;&lt; obj1.val , &quot;original object&quot;&gt;&gt;&gt;;<br>
</blockquote>
<br></div>
So one way to think about it, is that this is the same as C/C++<br>
  void fun void testclasspassing(test* i) {<div class="im"><br>
    4 =&gt; i-&gt;val;<br>
    &lt;&lt;&lt; i-&gt;val, &quot;in function&quot; &gt;&gt;&gt;;<br>
    // side effect!<br>
  }<br></div>
which in C++ can also be written as<div class="im"><br>
  fun void testclasspassing(test&amp; i)<br>
  {<br>
    4 =&gt; i.val;<br>
    &lt;&lt;&lt; i.val, &quot;in function&quot; &gt;&gt;&gt;;<br>
    // side effect!<br>
  }<br></div>
only that in chuck, the extra reference symbols are hidden away by the implementation of the reference object.<br>
<br>
So when mutating the object within the function, as it is a reference to an object on the heap, it will be mutated there too.<br>
<br>
By contrast, when one has a copy over type, a copy will be made, which will placed in the function stack. So when changing that within the function, that is a copy, and not the original object.<br><font color="#888888">
<br>
  Hans</font><div><div></div><div class="h5"><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>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Release me, insect, or I will destroy the Cosmos!<br>
</div>