D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12433 - Allow forward referencing IFTI types in template argument list
Summary: Allow forward referencing IFTI types in template argument list
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-21 11:17 UTC by Vladimir Panteleev
Modified: 2024-12-13 18:18 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2014-03-21 11:17:57 UTC
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 hsteoh 2014-09-25 20:26:19 UTC
Related: issue #10228
Comment 2 Seb 2016-05-06 11:01:10 UTC
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 dlangBugzillaToGithub 2024-12-13 18:18:32 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18798

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB