Uncallable delegates
Dukc
ajieskola at gmail.com
Mon Jun 22 17:57:22 UTC 2026
On Saturday, 20 June 2026 at 06:39:17 UTC, Walter Bright wrote:
> On 6/18/2026 9:35 AM, Dukc wrote:
>> 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.
>
> The trouble is there is no way to tell if the `char*` return
> value has anything to do with the delegate context pointer or
> not.
Assuming you meant `int*`. Wrong.
You do not have to know that. As long as the delegate call cannot
mutate the context itself, it also cannot return a mutable
pointer to it. If you have `const(int* delegate() @safe const)`,
then either:
- The return value is not pointing to the context, or
- The returned pointer was found from outside the context
already typed as mutable.
A delegate typed as `const(int* delegate() @safe)` could really
be returning a pointer to it's own context, that's one reason I'm
proposing making it uncallable. But note that `const(void
delegate() @safe)` must also be uncallable, because the call
itself could cause the context to get mutated.
We thus do not have to consider whether the return value could be
used to break the type system. By preventing the delegate call
from doing that, we're automatically making the return value also
safe.
More information about the dip.development
mailing list