Thread overview
Function parameter type inference: is this example working as intended?
Sep 05, 2018
Nathan S.
Sep 05, 2018
Nathan S.
Sep 05, 2018
Jonathan M Davis
September 05, 2018
The below writes "uint". Is this working as intended?
https://run.dlang.io/is/Dx2e7f

---
import std.stdio;

auto foo(T = uint)(uint x)
{
    return T.stringof;
}

auto foo(T = ulong)(ulong x)
{
    return T.stringof;
}

void main()
{
    writeln(foo(10L));
}
---
September 05, 2018
On Wednesday, 5 September 2018 at 02:33:47 UTC, Nathan S. wrote:
> The below writes "uint". Is this working as intended?
> https://run.dlang.io/is/Dx2e7f

Note that it's called not with a `ulong` literal but with a `long` literal.

September 04, 2018
On Tuesday, September 4, 2018 8:33:47 PM MDT Nathan S. via Digitalmars-d- learn wrote:
> The below writes "uint". Is this working as intended? https://run.dlang.io/is/Dx2e7f
>
> ---
> import std.stdio;
>
> auto foo(T = uint)(uint x)
> {
>      return T.stringof;
> }
>
> auto foo(T = ulong)(ulong x)
> {
>      return T.stringof;
> }
>
> void main()
> {
>      writeln(foo(10L));
> }
> ---

Probably, though personally, I'm inclined to think that it should pick the second. It's clearly using VRP to decide that 10L fits in a uint. If you pick something like

     writeln(foo(9999999999L));

then it will call the ulong overload. Either way, since a long is neither a uint or a ulong, it has to pick which overload it thinks is better. It would seem to me that ulong would be closer (and if VRP weren't involved, it would pick the ulong overload), but given that it knows what the value is, it knows that either can work.

Unfortunately, searching through the spec, I don't see anything in it mentioning either VRP or Value Range Propagation, so it doesn't look like the correct behavior is actually spec-ed out. It could be that it works the way it does in this case because of an intentional choice, or it could just be the consequence of logic choices that make sense in other contexts but not so much here. Personally, I would argue that VRP shouldn't come into play if it's not necessary, but I don't know what the actual rules for it are.

I suggest that you report this as a potential bug. At minimum, the spec needs to be updated with the rules for VRP.

- Jonathan M Davis