Uncallable delegates
Meta
jared771 at gmail.com
Sat May 16 06:31:07 UTC 2026
On Saturday, 16 May 2026 at 06:22:18 UTC, Timon Gehr wrote:
> On 5/16/26 06:40, Meta wrote:
>> I guess the solution is that `immutable(int* delegate())`
>> should become `int* delegate() immutable`.
>
> No.
>
> Here is how D catches the same problem for classes:
>
> ```d
> @safe
> class C{
> int* x;
> this(int* x)pure{ this.x=x; }
> int* foo(){ return x; }
> }
>
> C foo()pure => new C(new int(2));
>
> void main(){
> immutable c = foo(); // ok
> c.foo(); // error
> }
> ```
>
> ```
> Error: mutable method `tt.C.foo` is not callable using a
> `immutable` object
> ```
>
>
> However, delegates are actually not exactly > the same case as
> classes.
Why not? All the way down to the bedrock of CS theory, these are
equivalent cases.
> Calling `immutable(T delegate(S))` would be sound if there were
> no safe way to convert mutable references to `immutable`
> references.
Sure, but in D, a way does exist, so it's a moot point.
> The existence of `pure` factory functions means that in this
> case delegates do have to behave like classes. It is a global
> interaction.
Is your example not pretty much exactly equivalent to the fix I
proposed? What are you disagreeing with me about?
More information about the dip.development
mailing list