Uncallable delegates
Timon Gehr
timon.gehr at gmx.ch
Sat May 16 06:22:18 UTC 2026
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.
Calling `immutable(T delegate(S))` would be sound if there were no safe
way to convert mutable references to `immutable` references. The
existence of `pure` factory functions means that in this case delegates
do have to behave like classes. It is a global interaction.
More information about the dip.development
mailing list