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