www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cannot use local __lambda1 as parameter to non-global template

reply aliak <something something.com> writes:
Can someone help me understand the details around why this fails?

import std;

struct W(T) {
     T value;
     auto hook(handlers...)() {
         return handlers[0](value);
     }
}

template f(handlers...) {
     auto ref f(T)(auto ref T value) {
         return value.hook!handlers;
     }
}

 nogc void main() {
     auto a = W!int(3);
     auto b = a.f!((_) => "yes");
}

If I specifically type the lambda I pass in to f (i.e. (int _) => 
"yes") then it works. Or if I make hook a global template that 
takes a W!T as the first parameter it also works. Is there a work 
around for this?
Jul 26 2019
parent reply aliak <something something.com> writes:
On Friday, 26 July 2019 at 16:20:10 UTC, aliak wrote:
 Can someone help me understand the details around why this 
 fails?

 import std;

 struct W(T) {
     T value;
     auto hook(handlers...)() {
         return handlers[0](value);
     }
 }

 template f(handlers...) {
     auto ref f(T)(auto ref T value) {
         return value.hook!handlers;
     }
 }

  nogc void main() {
     auto a = W!int(3);
     auto b = a.f!((_) => "yes");
 }

 If I specifically type the lambda I pass in to f (i.e. (int _) 
 => "yes") then it works. Or if I make hook a global template 
 that takes a W!T as the first parameter it also works. Is there 
 a work around for this?
Err sorry, that was ldc pre the 5710 bug fix. In DMD I get this: Error: nogc function D main cannot call non- nogc function onlineapp.main.f!(W!int).f I guess it's allocating a delegate somewhere. Can I fix that?
Jul 26 2019
parent aliak <something something.com> writes:
On Friday, 26 July 2019 at 16:21:52 UTC, aliak wrote:
 On Friday, 26 July 2019 at 16:20:10 UTC, aliak wrote:
 Can someone help me understand the details around why this 
 fails?

 import std;

 struct W(T) {
     T value;
     auto hook(handlers...)() {
         return handlers[0](value);
     }
 }

 template f(handlers...) {
     auto ref f(T)(auto ref T value) {
         return value.hook!handlers;
     }
 }

  nogc void main() {
     auto a = W!int(3);
     auto b = a.f!((_) => "yes");
 }

 If I specifically type the lambda I pass in to f (i.e. (int _) 
 => "yes") then it works. Or if I make hook a global template 
 that takes a W!T as the first parameter it also works. Is 
 there a work around for this?
Err sorry, that was ldc pre the 5710 bug fix. In DMD I get this: Error: nogc function D main cannot call non- nogc function onlineapp.main.f!(W!int).f I guess it's allocating a delegate somewhere. Can I fix that?
I can do this but ugh: import std; struct W(T) { T value; static auto hook(handlers...)(auto ref typeof(this) self) { return handlers[0](self.value); } } template f(handlers...) { auto ref f(T)(auto ref T value) { return value.hook!handlers(value); } } nogc void main() { auto a = W!int(3); auto b = a.f!((_) => "yes"); }
Jul 26 2019