I think you can pretty much say that ChucK follows Java's attitude towards objects, where the declaration of any non-primitive always contains a pointer to the memory where the object's data is stored, as opposed to containing the acual values. That's why this doesn'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 => a1.x;</div><div>A a2;</div><div>a1 => a2;</div><div>2 => a2.x;</div><div><<< "a1.x=", a1.x, " a2.x=", a2.x>>>;</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 '=>' on types 'A' and 'A'...</div>
<div>[<a href="http://test.ck">test.ck</a>]:line(8): ...(note: use '@=>' for object reference assignment)</div><div><br></div><div>After I'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'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'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 <stdio.h></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> *(&(a1.x) +4) = 2;</div><div> // this may print "a1.x=1 a2.x=2" if the alignment is correct</div><div> printf("a1.x=%d a2.x=%d\n", 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 @=> 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"><<a href="mailto:haberg@math.su.se">haberg@math.su.se</a>></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'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 => int val;<br>
}<br>
<br>
test obj1;<br>
<<< obj1.val , "original object">>>;<br>
<br>
fun void testclasspassing(test i)<br>
{<br>
4 => i.val;<br>
<<< i.val, "in function" >>>;<br>
// side effect!<br>
}<br>
<br>
testclasspassing(obj1);<br>
<<< obj1.val , "original object">>>;<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 => i->val;<br>
<<< i->val, "in function" >>>;<br>
// side effect!<br>
}<br></div>
which in C++ can also be written as<div class="im"><br>
fun void testclasspassing(test& i)<br>
{<br>
4 => i.val;<br>
<<< i.val, "in function" >>>;<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>