Named Arguments Status Update - Overloading by name

Walter Bright newshound2 at digitalmars.com
Tue Jan 9 00:08:06 UTC 2024


On 1/5/2024 1:48 AM, Dennis wrote:
> ## Overloading by name
> 
> With named arguments, you can disambiguate an overload with identical types by 
> name:
> ```D
> string f(T)(T x) { return "x"; }
> string f(T)(T y) { return "y"; }
> static assert(f(x: 0) == "x");
> static assert(f(y: 0) == "y");
> ```
> 
> However, both template functions will end up with exactly the same types. DIP 
> 1030 specifies parameter names aren't part of the mangling, resulting in 
> clashing symbols at run time:
> 
> ```D
> void main()
> {
>      writeln(f(x: 1)); // x
>      writeln(f(y: 1)); // also x
> }
> 
> ```
> 
> Should the compiler, after finding a matching overload, retry all other 
> overloads without named arguments to prevent this? Or should it instantiate it 
> the `x` variant because it saw it first, and then refuse to instantiate `y` 
> because the mangle has been seen before?

```
string f(T)(T x) { return "x"; }
string f(T)(T y) { return "y"; }
```

should give an error. The order should not matter. Just like:

```
string f(int x) { return "x"; }
string f(int y) { return "y"; }
```

gives this error:

Error: function `test3.f(int y)` conflicts with previous declaration at test3.d(2)

I.e. as you correctly observed, the function/template signature is not affected 
by the parameter names, therefore two functions/templates definitions with the 
same signature are an error.


More information about the Digitalmars-d mailing list