Passing function as values and returning functions

Xan xancorreu at gmail.com
Wed Apr 11 04:10:54 PDT 2012


Hi,

Following the thread of Higher-order functions, how can I do to 
pass a function as a parameter and return a function. That is a 
something like:

import std.functional, std.stdio;

int f (int a) {
    return 2*a;
}

int delegate (int) g(int function(int a) p) {
    return p;
}


void main() {
    writeln(g(f)(1));
}


but it gives me:

$ gdmd-4.6 functions.d
functions.d:8: Error: cannot implicitly convert expression (p) of 
type int function(int a) to int delegate(int)
functions.d:13: Error: function functions.f (int a) is not 
callable using argument types ()
functions.d:13: Error: expected 1 function arguments, not 0
functions.d:13: Error: function functions.g (int function(int a) 
p) is not callable using argument types (int)
functions.d:13: Error: cannot implicitly convert expression (f()) 
of type int to int function(int a)


Thanks,
Xan.


More information about the Digitalmars-d-learn mailing list