[chuck-users] variable of type function
Spencer Salazar
ssalazar at princeton.edu
Mon Jul 17 12:31:03 EDT 2006
Yo!
On Jul 17, 2006, at 6:01 AM, Atte André Jensen wrote:
> Spencer Salazar wrote:
>> Hey Atte,
>> I dont think its possible to store references to functions, or
>> function pointers (a four letter word these days :P )
>
> Is function pointers considered bad? If so, why?
Not to get bogged down in semantics, but its not an uncommon belief
that _pointers_ in general are dangerous, in contrast with
_references_, which are safe. There are safe ways to do function
references, but ChucK doesnt seem to employ them at the moment.
In light of that, I still think there are ways to accomplish what you
want without them. Here is one idea:
- rename Instrument.sine() to something suitably generic, like
Instrument.play() (this isnt absolutely necessary but makes things
clearer)
- extend Instrument for each new instrument you want to define, and
override the sine()/play() function
- maintain a Instrument reference in your sequencer class, and spork
the Instrument.play() function. The play() function that is actually
called will depend on which subclass is held by the Instrument
reference.
There is one problem with this. ChucK apparently doesnt allow
overriding static member functions in subclasses. This is not
documented, but it should be, if it is in fact the case. But one can
hack around this as follows:
- make the sine()/play() functions all non-static
- since currently its not possible to spork non-static member
functions, create a new function in the Instrument superclass, fun
static void _play( Instrument @ i ) { i.play( ... ); }. In your
sequencer, you can spork the _play() function, and the appropriate
(non-static) play() function will be called. This is just the
"sporking non-static member functions" hack mentioned earlier on the
list.
hope this helps,
spencer
>> but you can
>> achieve something very similar with objects and polymorphism.
>
> Not as far as I can see (in my case).
>
> The function I'd like to reference to is inside an instrument class.
> Each member function is an instrument, so it's something like:
>
> public class Instruments
> {
> fun static void sine(float note, float dynamic, dur length){
> 1 => float volume;
> sinosc osc => ADSR env => dac;
> dynamic => osc.gain;
> std.mtof(note) => osc.freq;
> 1 => env.keyOn;
> length => now;
> 1 => env.keyOff;
> .1::second => now;
> }
> }
>
>
> Then I have a sequencer class that essentially steps through an
> array of
> notes and spork ~ Instruments.sine() for every entry in the array. For
> the sequencer to be able to use any of my instruments defined in
> Instruments class I need to do:
>
> public class Seq{
> function instrument;
>
> //later...
> spork ~ instrument();
>
> setFun(function new_instrument){
> new_instrument => instrument;
> }
> }
>
> Seq s;
> s.setFun(Instruments.sine);
>
> --
> peace, love & harmony
> Atte
>
> http://www.atte.dk | quartet: http://www.anagrammer.dk
> http://www.atte.dk/gps | compositions: http://www.atte.dk/
> compositions
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
More information about the chuck-users
mailing list