Uncallable delegates
Dukc
ajieskola at gmail.com
Thu Jun 18 16:35:39 UTC 2026
On Wednesday, 17 June 2026 at 17:34:55 UTC, Walter Bright wrote:
> On 6/17/2026 3:16 AM, Timon Gehr wrote:
>> Applying `const` to a delegate is fine. Calling a delegate
>> that may require a mutable context with a `const` context is
>> not. You can't pass a `const` argument with indirections to a
>> mutable parameter. Except that for delegates, this is
>> currently not enforced. Dukc's DIP fundamentally just proposes
>> consistently enforcing this basic rule.
>
> I do not understand how the DIP would work for this example.
Your instrumented example would compile and run like it does now,
since you are not actually calling the `const(int* delegate()
@safe)` delegate. However, Timon's example would result in the
compiler complaining for line `auto p = ps.dg();` that you're
attempting to call an uncallable delegate.
In unsafe code, the compiler would still accept `auto p =
(cast(int* delegate()) ps)();` or `auto p = (cast(const(int*
delegate() const)) ps)()` instead, but then you must manually
make sure that neither the delegate nor the context can get
mutated.
>
> DIP1000 can transfer the attributes of the implicit this to the
> return type.
He's talking about `immutable`, `const`, `shared` and `inout`.
The DIP is not proposing any changes to `return`/`scope`/`ref` or
their combinations.
More information about the dip.development
mailing list