[chuck-users] Passing classes to functions

Hans Aberg haberg at math.su.se
Sun Oct 4 07:17:43 EDT 2009


On 4 Oct 2009, at 11:40, Stefan Blixt wrote:

> 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.

I like to think on it the other way around:

Everything in the computer must be stored somewhere, and so they are  
objects with addresses. But if the address can be ignored, say by a  
copy-over procedure, they become data or values.

Or think of something physical, like a paper and what is written on  
it. The paper can't be copied itself, so it is an object. But if  
information on it can be copied over by some procedure, that becomes  
data.

> That's why this doesn't compile:
>
> class A {
>         int x;
> }
>
> A a1;
> 1 => a1.x;
> A a2;
> a1 => a2;
> 2 => a2.x;
> <<< "a1.x=", a1.x, " a2.x=", a2.x>>>;
>
> Hailstone:~ stefanblixt$ chuck test.ck
> [test.ck]:line(8): cannot resolve operator '=>' on types 'A' and  
> 'A'...
> [test.ck]:line(8): ...(note: use '@=>' for object reference  
> assignment)
>
> After I've put the @ in there as the error message suggests, I get  
> this output from the program:
>
> a1.x= 2  a2.x= 2

That is because
   a1 @=> a2;
sets a2 to point at whatever a1 points at.

I think Chuck could simplify and let => be applicable to objects where  
referencing is the default. Then for objects that support both copying  
and referencing, one might have spcial operators @=> and something for  
copying.

> 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 clone method in  
> java.lang.Object.

One also distinguishes between a clone: making a new copy of the  
object pointed to, and deep clone: if the object and the subobjects  
contain references, make copies of them too

> 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:
>
> #include <stdio.h>
>
> struct {
> int x;
> } a1;
> struct {
> int x;
> } a2;
>

In C/C++, these are copy-over object: a compiler implementation  
defined chunk of memory that is copied over in full. Chuck implements  
for a class A something like (pseudocode)
   template<class A>
   ref<A> {
     A* p_ = 0;
     ...
   };

> int main(char** argv) {
>         a1.x = 1;
>         a2.x = 3;
>         *(&(a1.x) +4) = 2;
>         // this may print "a1.x=1 a2.x=2" if the alignment is correct
>         printf("a1.x=%d a2.x=%d\n", a1.x, a2.x);
> }

(Though the behavior is entirely compiler dependent. You can get  
around it by making an array and using sizeof() though, I think.)

> The confusing bit in ChucK is that it allows declarations with or  
> without a @ before the variable name. My interpretation of this is  
> that
>
> A a;
>
> should be seen as a kind of shorthand for this:
>
> new A @=> A @ a;
>
> These two staments are identical, I think.


Yes. That seems to be the case.

   Hans




More information about the chuck-users mailing list