why is this cast required?
DanielG
simpletangent at gmail.com
Sat Sep 26 16:49:48 UTC 2026
I appreciate the reply, but that's not the question I am asking.
I am asking why there's a difference between an explicitly typed
variable, and an explicitly typed function argument - both of
which 'receive' the mixed-type array literal.
The compiler will implicitly upgrade the function pointers to
delegates in one case, but not the other. What is the basis, or
philosophy behind this policy?
Why do variable assignments receive this treatment, but not
function arguments?
IMO this violates the Principle of Least Astonishment.
```d
alias IntFunc = int delegate();
void main()
{
int two() => 2;
// automatically upgrades function pointers to delegates
// which is exactly what we'd expect ...
IntFunc[] explicit = [
() => 1,
() => two()
];
void func(IntFunc[] arg) {
// but in the case of a function,
// our VERY EXPLICIT type parameter is seemingly ignored.
// there is no coercion/upgrading of function pointers to
delegates
// at the call site, as with the variable.
// BUT WHY?
// what is the reasoning?
}
}
```
More information about the Digitalmars-d-learn
mailing list