[chuck-users] Passing classes to functions

Robert Poor rdpoor at gmail.com
Fri Oct 2 10:57:44 EDT 2009


Art:

Maybe I don't understand your intention, but the code works as I would
expect.  Consider these lines:

[1] test obj1;
[2] // obj2 is a reference to obj1
[3] test obj2 @=> obj1;

Line [1] instantiates a test object.  Line [3] instantiates second test
object and clobbers the obj1 reference, with the result that obj1 and obj2
really and truly reference the same piece of memory.  Changing the contents
of "the thing that obj1 references" will also change the contents of "the
thing that ob2 references", since they are the same object.

Thus the results that I see appear correct:

bash-3.2$ chuck adam.ck
0 original object
0 reference to original
2 original object
2 reference to original
2 in function
2 in function

Am I missing something?

- Rob
On Fri, Oct 2, 2009 at 00:38, Adam Tindale <adamtindale at hotmail.com> wrote:

> Hi All,
>
> I was doing some work and created some bugs that I ironed out when I
> discovered something: when non-primitive types are passed to functions
> they are passed by reference and not by value! I include a test so you
> can see what I mean.
>
> I hope somebody find this useful.
>
> --art
>
> class test{
>  0 => int val;
> }
>
> test obj1;
> // obj2 is a reference to obj1
> test obj2 @=> obj1;
>
> <<< obj1.val , "original object">>>;
> <<< obj2.val , "reference to original">>>;
>
> 2 => obj2.val;
>
> <<< obj1.val , "original object">>>;
> <<< obj2.val , "reference to original">>>;
>
> fun void testclasspassing(test i)
> {
>    <<< i.val, "in function" >>>;
> }
>
> testclasspassing(obj1);
> testclasspassing(obj2);
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20091002/eeae40b9/attachment.htm>


More information about the chuck-users mailing list