www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20091] New: nogc callback inferred as non-nogc


          Issue ID: 20091
           Summary: nogc callback inferred as non-nogc
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ali.akhtarzada gmail.com

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

Produces: Error:  nogc function D main cannot call non- nogc function

Adding  nogc on function f says hook!((_) => "yes").hook closes over variable

If you explicitly specify a type on the lambda that you pass to f then it

auto b = a.f!((int) => "yes");

Jul 29 2019