Thread overview | |||||
---|---|---|---|---|---|
|
April 10, 2016 Lazy evaluation of function pointers. | ||||
---|---|---|---|---|
| ||||
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 |
April 10, 2016 Re: Lazy evaluation of function pointers. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ryan Frame | 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.
|
April 10, 2016 Re: Lazy evaluation of function pointers. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Parrill | 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
|
Copyright © 1999-2021 by the D Language Foundation