www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23664] New: Infer `const` for lambdas/closures

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

          Issue ID: 23664
           Summary: Infer `const` for lambdas/closures
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

Attributes (` safe`, `pure`, etc.) are inferred for lambdas, but a delegate can
also specify a type constructor (or a combination such as `inout const`).
Because the implementation of the lambda is always known, the compiler can
figure out if the delegate is in fact

* `immutable`, i.e. all data it accesses through its context (if any) is
`immutable`.
* `const`, i.e. all the accessed data in its context (if any) is not changed by
the delegate (think of: the delegate only calls free functions and `const`
member functions on things it accesses).

Note that type constructors in this case are purely additional information, the
same way ` safe`, `pure`, `nothrow`, or ` nogc`. This means that forgetting
them won’t make using the delegate invalid; only the context it’s used in
might
require them. (This requires that the implicit conversion of `immutable` to
`const` and `immutable`/`const` to mutable for delegate context type
constructors is implemented.

--
Feb 02 2023