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

Quirin Schroll qs.il.paperinik at gmail.com
Tue Jun 20 14:40:57 UTC 2023


On Saturday, 17 June 2023 at 18:37:48 UTC, Paul Backus wrote:
> On Friday, 16 June 2023 at 15:29:47 UTC, Quirin Schroll wrote:
>> 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.

That’s probably the case, but it’s not a good reason. A function 
taking a `long` isn’t called the same as a function taking an 
`int` (different sizes), still I can call that function with an 
`int`; the value can be converted. Having to do an explicit 
conversion would be annoying and serve no purpose. The same is 
true for `function` → `delegate`. And there isn’t even an 
explicit conversion the likes of `cast(DelegateType) fp`.


More information about the Digitalmars-d mailing list