Hi Is it possible to have a variable of type function? If so, could someone provide a link to where I can read about it or a short example showing how it works? Will it work with overload definitions? -- peace, love & harmony Atte http://www.atte.dk | quartet: http://www.anagrammer.dk http://www.atte.dk/gps | compositions: http://www.atte.dk/compositions
Atte André Jensen wrote:
Hi
Is it possible to have a variable of type function?
Or clearer, more correctly put: "Is it possible to store the reference to a function in a variable?" I'd like to do something like this: class test{ f function; fun void set(function new_f){ f @=> new_f; } fun void print(){ f(); } } fun void print_a(){ <<<"a">>>; } fun void print_b(){ <<<"b">>>; } test c; c.set(print_a); c.f(); //should print "a"; c.set(print_b); c.f(); //should print "b"; -- peace, love & harmony Atte http://www.atte.dk | quartet: http://www.anagrammer.dk http://www.atte.dk/gps | compositions: http://www.atte.dk/compositions
On Sun, 16 Jul 2006 15:29:40 +0200, "Atte André Jensen"
Atte André Jensen wrote:
Hi
Is it possible to have a variable of type function?
I think this is covered on the "what is SucK" wiki page :) http://wiki.cs.princeton.edu/index.php/What_is_SucK See: "ChucK without function pointers (already the case, ouch)." Indeed, this would be really useful and would lead to more general coding. Perhaps Ge et. al. would know where this lies on the roadmap? -Scott -- http://www.fastmail.fm - Email service worth paying for. Try it for free
Hey Atte, I dont think its possible to store references to functions, or function pointers (a four letter word these days :P ), but you can achieve something very similar with objects and polymorphism. Essentially, you encapsulate your function inside a class and then extend that class as necessary when you want to make a new function to refer to. As a trivial example: class F_int_void { fun int f() { return 0; } } class myFunc extends F_int_void { fun int f() { return 1; } } class myOtherFunc extends F_int_void { fun int f() { return 2; } } F_int_void @ fiv; F_int_void a; a @=> fiv; <<< fiv.f(), "" >>>; myFunc b; b @=> fiv; <<< fiv.f(), "" >>>; myOtherFunc c; c @=> fiv; <<< fiv.f(), "" >>>; spencer On Jul 16, 2006, at 9:29 AM, Atte André Jensen wrote:
Atte André Jensen wrote:
Hi
Is it possible to have a variable of type function?
Or clearer, more correctly put: "Is it possible to store the reference to a function in a variable?"
I'd like to do something like this:
class test{ f function;
fun void set(function new_f){ f @=> new_f; }
fun void print(){ f(); }
}
fun void print_a(){ <<<"a">>>; }
fun void print_b(){ <<<"b">>>; }
test c; c.set(print_a); c.f(); //should print "a";
c.set(print_b); c.f(); //should print "b";
-- 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@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
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?
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
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@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
participants (3)
-
Atte André Jensen
-
Scott Davidson
-
Spencer Salazar