[Issue 23918] New: Lambdas declared as "function" should be static and not have a context

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 12 21:21:41 UTC 2023


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

          Issue ID: 23918
           Summary: Lambdas declared as "function" should be static and
                    not have a context
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: dlang-bugzilla at thecybershadow.net

/////////////////// test.d ///////////////////
struct BobsContext
{
    void bobWhoAlreadyHasAContext(alias fun)()
    {
        fun(1);
    }
}

void main()
{
    BobsContext b;

    // Works:
    {
        void fun(X)(X x) {}
        b.bobWhoAlreadyHasAContext!fun();
    }

    // Doesn't work:
    {
        alias fun = function (x) {};
        b.bobWhoAlreadyHasAContext!fun();
    }
}
//////////////////////////////////////////////

(DMD currently produces the "function requires a dual-context" deprecation)

Leaving aside the question of why lambdas don't have their need for context
auto-detected like normal template functions... if the user writes the keyword
"function", that should effectively make the lambda static and not require a
context.

If this somehow ends up too much of a breaking change... the "static" keyword
is currently not accepted in lambda / anonymous function literals, but it could
be.

--


More information about the Digitalmars-d-bugs mailing list