Named Arguments Status Update - Overloading by name
Dennis
dkorpel at gmail.com
Thu Jan 11 10:40:57 UTC 2024
On Tuesday, 9 January 2024 at 00:08:06 UTC, Walter Bright wrote:
> ```
> string f(T)(T x) { return "x"; }
> string f(T)(T y) { return "y"; }
> ```
>
> should give an error. The order should not matter.
> (...)
> the function/template signature is not affected by the
> parameter names, therefore two functions/templates definitions
> with the same signature are an error.
Depending on what you consider the 'signature', that's either not
sufficient reason to raise an ambiguity error, or not sufficient
prevention of conflicts. Consider that the same 'signature' can
be distinguished by constraints:
```D
string f(T)(T x) if (T.sizeof <= 2) { return "x"; }
string f(T)(T y) if (T.sizeof >= 2) { return "y"; }
```
This is allowed and works like this:
```D
pragma(msg, f(byte(0))) // x
pragma(msg, f(int(0))) // y
pragma(msg, f(short(0))) // error, `(short)` matches both
templates
```
But with named arguments:
```D
pragma(msg, f(x: short(0))) // x, `(short)` matches x
pragma(msg, f(y: short(0))) // y, `(short)` matches y
```
Detecting potential overlap upfront is both a breaking change and
mathematically impossible.
More information about the Digitalmars-d
mailing list