[chuck-users] Cloning
Hans Aberg
haberg at math.su.se
Fri Dec 11 09:05:31 EST 2009
ChucK does not have cloning for reference object, right? That is, the
semantic equivalent of an this explicit copy-constructor pseudocode:
class A {...}
A a0;
...
new A(a0) @=> A a1;
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).
It is a problem in the program I wrote, scale.ck: 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.
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.
Hans
---- clone.ck ----
class A {
string s;
}
A a; "a" @=> a.s;
A b; "b" @=> b.s;
chout <= "a.s = " <= a.s <= ", b.s = " <= b.s <= ".\n";
"c" @=> b.s;
chout <= "\"c\" @=> b.s; a = " <= a.s <= ", b = " <= b.s <= ".\n";
a.s @=> b.s;
chout <= "a.s @=> b.s; a.s = " <= a.s <= ", b.s = " <= b.s <= ".\n";
"d" @=> b.s;
chout <= "\"d\" @=> b.s; a.s = " <= a.s <= ", b.s = " <= b.s <= ".\n";
a @=> b;
chout <= "a @=> b; a.s = " <= a.s <= ", b.s = " <= b.s <= ".\n";
"e" @=> b.s;
chout <= "\"e\" @=> b.s; a.s = " <= a.s <= ", b.s = " <= b.s <= ".\n";
---- run ----
$ chuck clone.ck
a.s = a, b.s = b.
"c" @=> b.s; a = a, b = c.
a.s @=> b.s; a.s = a, b.s = a.
"d" @=> b.s; a.s = a, b.s = d.
a @=> b; a.s = a, b.s = a.
"e" @=> b.s; a.s = e, b.s = e.
--------
More information about the chuck-users
mailing list