www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Lazy evaluation of function pointers.

reply Ryan Frame <dlang ryanjframe.com> writes:
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
Apr 10 2016
parent reply Alex Parrill <initrd.gz gmail.com> writes:
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.
Apr 10 2016
parent Ryan Frame <dlang ryanjframe.com> writes:
On Sunday, 10 April 2016 at 19:02:06 UTC, Alex Parrill wrote:
 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.
That makes sense. Thank you. --Ryan
Apr 10 2016