why is this cast required?
Tannesover
jimmdox at proton.me
Thu Oct 8 03:16:15 UTC 2026
On Friday, 25 September 2026 at 11:14:50 UTC, DanielG wrote:
> ```d
> alias IntFunc = int delegate();
>
> void exec(IntFunc[] funcs) {}
>
> void main()
> {
> int two() => 2;
>
> exec(
> // why do I have to cast to the exact type that exec()
> already takes?
> cast(IntFunc[])
> [
> () => 1,
> () => two(),
> ]);
> }
> ```
> https://slopegame-2.io
> Without the cast, the error is:
>
>>Error: ) incompatible types for `(() => 1) : (() => two())`:
>>`int function() pure nothrow @nogc @safe` and `int delegate()
>>pure nothrow @nogc @safe`
This is a classic issue with overload resolution in D. To clean
up the code without using an explicit cast, you can wrap the
array elements with std.algorithm.map!toDelegate or mark the
individual function pointer with toDelegate.
More information about the Digitalmars-d-learn
mailing list