Sorry the example I was looking at when I mentioned that was not really a
function reference but it gets the same result. It does so by using ChucK
class functionality that in other languages is called polymorphism. It is
described in ChucK language documentation.
http://chuck.cs.princeton.edu/doc/language
See the example class Xfunc in the inheritence section. What is done is to
create a base class with a standard class function (in the example called
doSomething().) For each different version of function that is needed a new
class is created which inherits the base class and overrides doSomething()
with the new code. The "calling function" is then declared with a base
class object as an argument. You can pass it any object that inherits the
base class (in this case Xfunc.) When it calls the doSomething() method it
invokes the overridden version from the derived class.
The example doesn't create a function to do this but it shows an example of
common code calling different functions from different classes. It does
this by creating an array containing objects from different classes and
then calling the different versions in a loop using the array. Clearly the
common code in the loop could have been a function.
I'm away from my ChucK machine at the moment but if anyone is interested I
could try to create a simple example which does the inner work (calling a
passed in function) in a function. As usual the documentation is vague but
I believe the function version of the example would look something like:
fun int callFunc(Xfunc @ in, int a, int b)
{
return in.doSomething(a,b);
}
// loop over the Xfunc
for( 0 => int i; i < operators.cap(); i++ )
{
// doSomething, potentially different for each Xfunc
<<< callerFunc(operators[i], 4, 5 ) >>>;
}
-steve
On Fri, Dec 27, 2013 at 7:03 PM, Dealga McArdle
re: Steve Morris
3) You can pass a reference to a function as an argument to another
function.
how? :) I haven't found how to do that anywhere.
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users