cannot use local __lambda1 as parameter to non-global template

aliak something at something.com
Fri Jul 26 16:23:52 UTC 2019


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- at 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");
}


More information about the Digitalmars-d-learn mailing list