Eduard;


My question, actually, should have been about polymorphism and not inheritance. What I want is that  having a base class Point and another class Point2D which inherits from Point, that I can define a Point  p, that could morph and become a Point2D. Or in other words, that an UGen, can become a SinOsc.

Is that possible?


Well, the thing is; You can assign a SinOsc to a object of type UGen because a SinOsc is a UGen. However, a UGen object doesn't have a .freq() member function so you can't use that with it, even though a SinOsc (which does have that function) has been asigned to it.

Let's take a practical example;
----------------8<-----------
UGen u;

if (maybe) new SinOsc @=> u;
else new Gain @=> u;

//so far so good


//here comes trouble;
440 => u.freq;
--------------8<----------------

That last line is the trouble-maker; it adresses a UGen and at that point we're not sure that UGen has that function, hence the parser objects. That line may well be valid but we don't know this for sure.

What I think you may need is a new class that can generate modulation signals, then extend that for various behaviours. This won't work like a UGen but you could connect it using a "dummy" Gain UGen in the parrent class that might serve as a output. How you'd do that in practice would depend on the exact scenario, for a extremely general case this could become somehwat lengthy and involved. For a extremely basic frame-work see below.

Yours,
Kas.

--------------8<-------------------
//this will parse. It may or may not be the kind of thing you are after.
//clearly a lot more infrastucture is needed

class Modulator
    {
    Gain out;
    }
   
class Foo extends Modulator
    {
    ADSR env => out;
    }
   
class Bar extends Modulator
    {
    Envelope env => out;
    }

Modulator m;
m.out => SinOsc s => dac;

new Foo @=> m;
-----8<----------------------

ps; you'll be fighting the type system. Typically fighting the type system leads to bug-reports. If it's 4am and you're throwing the keyboard against the wall do considder that you may be right and ChucK might be wrong.