digitalmars.D.learn - Lambda cannot access frame of function
- kerdemdemir (26/26) Nov 18 2017 //DMD64 D Compiler 2.072.2
- Adam D. Ruppe (4/5) Nov 18 2017 Change that `function` to `delegate` and it should work.
- kerdemdemir (4/10) Nov 18 2017 Yes it worked as you suggested.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (13/35) Nov 18 2017 foo should take 'delegate'.
//DMD64 D Compiler 2.072.2
import std.stdio;
bool foo( bool function( double ) controlFoo )
{
return controlFoo(5.0);
}
void foo2( double val )
{
writeln ( foo( a => a > val ) );
}
void main()
{
foo2(20);
writeln("Hello, World!");
}
Does not compile and gives this errors:
source_file.d(12): Error: function source.foo2.__lambda2 cannot
access frame of function source.foo2
source_file.d(12): Error: function source.foo (bool
function(double) controlFoo) is not callable using argument types
(void)
Live example:
http://rextester.com/WRKCWR55408
Is there any workaround for that?
Regards
Erdem
Nov 18 2017
On Saturday, 18 November 2017 at 14:22:19 UTC, kerdemdemir wrote:bool foo( bool function( double ) controlFoo )Change that `function` to `delegate` and it should work. Function pointers aren't allowed to access other locals, but delegates are.
Nov 18 2017
On Saturday, 18 November 2017 at 14:30:29 UTC, Adam D. Ruppe wrote:On Saturday, 18 November 2017 at 14:22:19 UTC, kerdemdemir wrote:Yes it worked as you suggested. Thanks.bool foo( bool function( double ) controlFoo )Change that `function` to `delegate` and it should work. Function pointers aren't allowed to access other locals, but delegates are.
Nov 18 2017
On 11/18/2017 06:22 AM, kerdemdemir wrote:
//DMD64 D Compiler 2.072.2
import std.stdio;
bool foo( bool function( double ) controlFoo )
{
return controlFoo(5.0);
}
void foo2( double val )
{
writeln ( foo( a => a > val ) );
}
void main()
{
foo2(20);
writeln("Hello, World!");
}
Does not compile and gives this errors:
source_file.d(12): Error: function source.foo2.__lambda2 cannot access
frame of function source.foo2
foo should take 'delegate'.
If you want it to work with any callable entity, take the function as an
alias template parameter:
bool foo(alias controlFoo)()
{
return controlFoo(5.0);
}
void foo2( double val )
{
writeln ( foo!( a => a > val )() );
}
Ali
Nov 18 2017









kerdemdemir <kerdemdemir gmail.com> 