Named Arguments Status Update - Overloading by name

Walter Bright newshound2 at digitalmars.com
Thu Jan 11 19:21:46 UTC 2024


On 1/11/2024 2:40 AM, Dennis wrote:
> 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.

It's ok if the error is detected after instantiation. It can be detected by 
testing to see if the mangled signature (which is generated for the type of the 
function) already exists.

The signature does not include parameter names nor the constraints. There must 
be a 1:1 correspondence between symbols and signature. The function's type is 
also its signature.

If these invariants do not hold, or if we make exceptions for them, the whole 
type system and assumptions about D fall apart in an unfixable manner.


More information about the Digitalmars-d mailing list