Re: D’s delegates — The good, the bad, and the ugly

Timon Gehr timon.gehr at gmx.ch
Sat Jun 17 22:18:42 UTC 2023


On 6/17/23 20:37, Paul Backus wrote:
> On Friday, 16 June 2023 at 15:29:47 UTC, Quirin Schroll wrote:
>> D’s delegates have a few issues, some are outright bugs and others are 
>> improvements that I wonder why they’re not in the language.
> 
> Excellent writeup. It's nice to have all of these in one place.
> ...

Regarding context qualifiers, there is also this issue:
https://issues.dlang.org/show_bug.cgi?id=20517

>> Another conversion that should Just Work is function pointer to 
>> delegate, in fact, because a function pointer has no context, its 
>> context is `immutable`:
> 
> I believe the reason this one doesn't work is that functions and 
> delegates have different calling conventions. In order to allow a 
> function pointer to convert to a delegate, the compiler would have to 
> generate a trampoline.

There's `std.functional.toDelegate` that generates the trampoline:
https://dlang.org/phobos/std_functional.html#toDelegate

However, it generates an unqualified context:

```d
import std.functional;

void foo()@safe{}

void main(){
     void delegate()immutable dg=toDelegate(&foo); // error
}
```

(Which it kind of has to because DMD incorrectly rejects the conversion 
from immutable to unqualified context.)


More information about the Digitalmars-d mailing list