[Issue 18587] New: Context pointer not set for delegate template parameter when declared in struct body

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 10 16:45:28 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18587

          Issue ID: 18587
           Summary: Context pointer not set for delegate template
                    parameter when declared in struct body
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: simen.kjaras at gmail.com

struct S {
    int n;
    R!(() => n) a;
}

struct R(alias fn) {
    int get() {
        return fn();
    }
}

unittest {
    auto s = S(3);
    assert(s.a.get() == 3);
}

The above code fails with 'Error: need 'this' for 'n' of type 'int''.
It is clear that the intended semantics of the above code is similar to this:

struct S {
   int n;
   auto a() { return R!(() => n)(); }
}

struct R(alias fn) {
    int get() {
        return fn();
    }
}

unittest {
    auto s = S(3);
    assert(s.a.get() == 3);
}

However, the latter example works, while the former doesn't. This leads to
unnecessary boilerplate and unreadable code.

--


More information about the Digitalmars-d-bugs mailing list