Passing function as values and returning functions

Xan xancorreu at gmail.com
Wed Apr 11 05:53:00 PDT 2012


On Wednesday, 11 April 2012 at 12:19:06 UTC, Steven Schveighoffer 
wrote:
> On Wed, 11 Apr 2012 08:08:44 -0400, Xan <xancorreu at gmail.com> 
> wrote:
>
>> On Wednesday, 11 April 2012 at 11:59:14 UTC, Jacob Carlborg 
>> wrote:
>
>>> Use "delegate" or "function" both for the argument type and 
>>> return type.
>>
>>
>> How to do that?
>
> int function(int) g(int function(int a) p) { return p; }
>
> Should do the trick.
>
> delegates are not implicitly convertible to/from function 
> pointers.
>
> You can, however, explicitly convert a function to a delegate.  
> But this should be done only when there is a requirement to use 
> delegates.
>
> However, use std.functional.toDelegate if you are interested.
>
> -Steve


Thanks, Steve, but another problem:

$ gdmd-4.6 functions.d
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) p) 
is not callable using argument types (int)
functions.d:13: Error: cannot implicitly convert expression (f()) 
of type int to int function(int)


import std.functional, std.stdio;

int f (int a) {
|___return 2*a;
}

int function(int) g(int function(int a) p) {
|___return p;
}


void main() {
|___writeln(g(f)(1));
}


I want to g return f and then f evaluate 1.





More information about the Digitalmars-d-learn mailing list