Lazy evaluation of function pointers.

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 10 12:02:06 PDT 2016


On Sunday, 10 April 2016 at 18:08:58 UTC, Ryan Frame wrote:
> Greetings.
>
> The following code works:
>
> void main() {
>     passfunc(&func);
> }
>
> void passfunc(void function(string) f) {
>     f("Hello");
> }
>
> void func(string str) {
>     import std.stdio : writeln;
>     writeln(str);
> }
>
> Now if I change passfunc's signature to "void passfunc(lazy 
> void function(string) f)" I would get the compiler error 
> "Delegate f () is not callable using argument types (string)". 
> I can lazily pass a void function() -- it seems that there is 
> only a problem when the function contains parameters.
>
> The only difference should be when the pointer is evaluated, so 
> why does lazy evaluation matter here?
>
> Thank you for your time
> --Ryan

A parameter declared as `lazy T` has the type `T delegate()`, 
which, when called, evaluates the expression that was passed into 
the function.

So effectively, this:

void foo(lazy int x) { auto i = x(); }
foo(a+b);

Is the same as this:

void foo(int delegate() x) { auto i = x(); }
foo(() => a+b);

T in your case is `void function(string)`. So you can do `auto 
func = f()` to get the function you passed in. It's not very 
useful in your example to lazily evaluate getting a function 
pointer, considering it's usually a constant expression after 
compiling.


More information about the Digitalmars-d-learn mailing list