Hi All,

Just getting into ChucK and have a language question.

I have some base classes which I have written and am struggling with how to get the inheritance to do what I want.

I have a class Voice, and a class Polysynth which contains an array of Voices as data, like so:

public class Polysynth {
{
        Voice voices[];
        // methods which work on the objects in 'voices',
        // ..
}

I want to be able to set up a patch which defines (for example) a MySynth_Voice (subclassing Voice) and MySynth (subclassing Polysynth). MySynth objects will thus contain an array of Mysynth_Voice objects. I want to then be able to call methods on MySynth that are defined in Polysynth, that uses the array of MySynth_Voices as data. The problem is, because they are expecting Voice objects, they aren't happy to receive MySynth_Voices instead (even though MySynth_Voice inherits from Voice). I can't cast them in the Polysynth methods that use it because it won't compile unless 'voices' is declared. It has to be declared as 'Voice', and if I try to override it in the subclass (eg. "MySynth_Voice voices[]"), it won't compile saying saying it "has already been defined in super class 'Polysynth'".

I tried a bunch of different ways to do this, but the only way I can find to do it was to cast the voices array in the subclass as follows.

class MySynth extends Polysynth
{
    fun void init (int num_voices) {
        MySynth_Voice voices_temp[num_voices];
        Voice voices_temp2[num_voices];
        for (0=>int i; i < num_voices; i++) {
            voices_temp[i] $ Voice @=> voices_temp2[i];
        }
        voices_temp2 @=> voices;
    }
}

Firstly, is it possible to do this without having to do this casting bit? It seems like a common inheritance set up, so am wondering if I have just missed something. Failing that, is there a better way to do the casting step? I tried to do in obvious ways like 'MySynth_Voice $ Voice[] @=> voices' but that, and other possible permutations I could think of, wouldn't compile. The iterating over the whole array and doing each member explicitly seemed to be the only thing that worked.

It's certainly no big deal and I can live with it, but am just curious if there's a nicer way to do it.

Thanks all...

-voytek

_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users

Reply via email to