ccrma / webchuck

ChucK on the Web
https://chuck.stanford.edu/webchuck
MIT License
42 stars 8 forks source link

cannot declare static non-primitive objects #11

Closed lmaxwell closed 1 year ago

lmaxwell commented 1 year ago

Hi, webchuck is cool!

I encountered an error when running my previous code that runs ok in chuck. The purpose is to access the same mixer in various different .ck files.

public class Mixer{
    static Bus @ bus[6];
    static FxRev @ fxrev;
   static FxDelay @ fxdelay;
   static Master @  master;
}

[]:line(69): cannot declare static non-primitive objects (yet)... []:line(69): ...(hint: declare as reference (@) & initialize outside class for now)

Will this be supported? Or is there alternative ways to achieve my purpose ?

terryzfeng commented 1 year ago

I tried to recreate your example in miniAudicle and got a similar message. I believe static Bus @ bus[6] should instead be static Bus @ bus[]. Including the [6] means that the bus object will be instantiated rather than just being a null reference (or pointing to the already declared static reference).

// Test
class Mixer {
    0.5 => float volume;    
}

class Channel {
    static Mixer @ mix[]; // reference, [] vs [2]
}

Mixer mainMix;
Mixer mix2;

new Channel @=> Channel c1;
[mainMix, mix2] @=> c1.mix; // initialization outside of class

<<< "c1 mix", c1.mix >>>;

new Channel @=> Channel c2;

<<< "c2 mix", c2.mix >>>;

I got the same result in WebChucK IDE. Let me know if that helps!

lmaxwell commented 1 year ago

Sorry, I should have posted my original code here, https://github.com/lmaxwell/guiless-daw/blob/main/lib/mixer/Mixer.ck . The original code works ok with version 1.3.6.0.

I started from your example and tried the newest version of ChucK(1.4.2.0) , it did throw the same error if specifying a number for a static array inside class definition, while ChucK(1.3.6.0) did not throw any error. However, in 1.3.6.0 (if a number is given in class definition) Channel.mix(accessing from class name) points to the same instance, while c1.mix and c2.mix(accessing from instance) point to different instances. It seems a bug and 1.4.2 sovles this.

The following code works fine in both chuck(1.4.2.0) and webchuck IDE.

//a.ck
class Mixer {
    0.5 => float volume;    
}

public class Channel {
    static Mixer @ mix[]; // reference, [] vs [2]
}

Mixer mainMix;
Mixer mix2;

[mainMix, mix2] @=> Channel.mix; // initialization outside of class

new Channel @=> Channel c1;

<<< "a.ck:Channel.mix", Channel.mix >>>;
<<<"a.ck:c1.mix", c1.mix>>>;
//b.ck
new Channel @=> Channel c2;
<<<"b.ck:Channel.mix",Channel.mix>>>;
<<<"b.ck:c2.mix",c2.mix>>>;

Thank you for your help!

terryzfeng commented 1 year ago

No problem! Glad to see it working!