www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23835] New: Accessing variable outside nested function

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

          Issue ID: 23835
           Summary: Accessing variable outside nested function creates
                    delegate even if it is static
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: alphaglosined gmail.com

This will error with: Error: function `onlineapp.main.__lambda2` cannot access
variable `builder` in frame of function `D main`

Builder.perform is static, which means no context pointer is required for
perform to be callable inside a nested function.

```d
alias Function = int function();

void main() {
    Builder builder;
    Function func = () {
        return builder.perform();
    };
    assert(func() == 2);
}

struct Builder {
    static int perform() {
        return 2;
    }
}
```

This should not be creating a delegate.

Possibly a duplicate of: https://issues.dlang.org/show_bug.cgi?id=10877

--
Apr 12 2023