Function parameter type inference: is this example working as intended?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Sep 5 04:33:34 UTC 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





More information about the Digitalmars-d-learn mailing list