Comment #0 by dlang-bugzilla — 2014-03-21T11:17:57Z
Consider this hypothetical example:
///////////////// test.d /////////////////
T[] copyArray(T=I, I)(I[] input)
{
auto result = new T[input.length];
foreach (n, ref i; input)
result[n] = i;
return result;
}
void main()
{
// copy as is
int[] r1 = copyArray([1, 2, 3]);
// copy to another type
long[] r2 = copyArray!long([1, 2, 3]);
}
//////////////////////////////////////////
There is currently no way to get this code to work, without either declaring an overload for copyArray, or replacing T=I with T=SomeDummyType and later checking to see if something was explicitly passed or not.
Comment #1 by hsteoh — 2014-09-25T20:26:19Z
Related: issue #10228
Comment #2 by greensunny12 — 2016-05-06T11:01:10Z
It's not an "hypothetical example" - it makes real code ugly! :/
Let me add an example from a recent PR in phobos
https://github.com/dlang/phobos/pull/4263
If we could support the forwarding of argument types, we would only have _one_, very nice & readable function header:
```
T[] makeArray(T = Unqual!(ElementType!R), Allocator, R)
```
Of course we can add another function overload, but it unnecessary bloats!
```
Unqual!(ElementType!R)[] makeArray(Allocator, R)(auto ref Allocator alloc, R range)
{
import std.range.primitives;
alias T = Unqual!(ElementType!R);
return makeArray!(T, Allocator, R)(alloc, range);
}
T[] makeArray(T, Allocator, R)(auto ref Allocator alloc, R range)
```
Comment #3 by robert.schadek — 2024-12-13T18:18:32Z