January 11

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:

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:

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:

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.

January 11
On 1/9/2024 5:25 AM, Timon Gehr wrote:
> I fully agree with your analysis above. This should error.

I will treasure this day forever. Timon agrees with me!

January 11
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.
January 11

On Thursday, 11 January 2024 at 19:21:46 UTC, Walter Bright wrote:

>

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.

That is the second option I listed in my opening post, which can be implemented easily.
However, it would create a compile time 'race condition': the f!short instantiation which dmd sees first may succeed, but any subsequent attempts will fail.

I don't know if this will result in inscrutable errors in practice, but it very well may.

January 11
On 1/11/24 20:21, Walter Bright wrote:
> 
> 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.

As I wrote in another post, I do not think this works, because one instantiation may be in one compilation unit while the other instantiation is in another compilation unit. They never exist both at once in the same compiler invocation but they will have the same mangled name yet be different.
January 11
On 1/11/24 20:13, Walter Bright wrote:
> On 1/9/2024 5:25 AM, Timon Gehr wrote:
>> I fully agree with your analysis above. This should error.
> 
> I will treasure this day forever. Timon agrees with me!
> 

Well, that is a regular occurrence, it's why I am a user of D.
January 14
On 1/11/2024 12:33 PM, Dennis wrote:
> On Thursday, 11 January 2024 at 19:21:46 UTC, Walter Bright wrote:
>> 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.
> 
> That is the second option I listed in my opening post, which can be implemented easily.
> However, it would create a compile time 'race condition': the `f!short` instantiation which dmd sees first may succeed, but any subsequent attempts will fail.
> 
> I don't know if this will result in inscrutable errors in practice, but it very well may.


string f(T)(T x) if (T.sizeof <= 2) { return "x"; }
string f(T)(T y) if (T.sizeof >= 2) { return "y"; }

Then the only thing to do is disallow an overload that differs only in the parameter names.
1 2 3
Next ›   Last »