Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow forward referencing IFTI types in template argument list #18798

Open
dlangBugzillaToGithub opened this issue Mar 21, 2014 · 2 comments
Open

Comments

@dlangBugzillaToGithub
Copy link

Vladimir Panteleev (@CyberShadow) reported this on 2014-03-21T11:17:57Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=12433

CC List

  • Seb

Description

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.
@dlangBugzillaToGithub
Copy link
Author

hsteoh commented on 2014-09-25T20:26:19Z

Related: issue #10228

@dlangBugzillaToGithub
Copy link
Author

greensunny12 commented on 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)	  
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant