www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23994] New: Compiler fails to derive lambda to be const

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

          Issue ID: 23994
           Summary: Compiler fails to derive lambda to be const
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andrej.mitrovich gmail.com

DMD 2.102.0

-----
module test;

void call(void delegate(int i) const cb) {
    cb(42);
}

struct C {
    void callback(int i) const { }
}

void main() {
    C c;
    call(some_int => c.callback(some_int));
}
-----

$ dmd test.d
 test.d(13): Error: function `test.call(void delegate(int i) const cb)` is not
callable using argument types `(void)
 test.d(13):        cannot pass argument `__lambda2` of type `void` to
parameter `void delegate(int i) const cb`
Commenting out `const` from both the parameter and the ballback function fixes the issue. Another workaround is to explicitly specify the callback type at the call site: ----- // Works with `const` with the example above call((int some_int) const => c.callback(some_int)); ----- But the compiler should be able to derive this on its own. This workaround also works, where the type is omitted: ----- call((some_int) const => c.callback(some_int)); ----- --
Jun 15 2023