Passing function as values and returning functions

Jacob Carlborg doob at me.com
Wed Apr 11 04:59:14 PDT 2012


On 2012-04-11 13:10, Xan wrote:
> 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)

Use "delegate" or "function" both for the argument type and return type.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list