Bug 9886 – Cannot pass .tupleof directly to a template
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-05T12:25:00Z
Last change time
2013-04-05T12:55:28Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2013-04-05T12:25:56Z
template Templ(T...) { alias Templ = T; }
struct S { int x, y; }
void main()
{
S s;
auto tup = s.tupleof;
auto x = Templ!(tup); // ok
auto y = Templ!(s.tupleof); // ng
}
For a use-case, a recently created 'reverse' function for the Phobos Tuple type had this implementation:
@property
auto reversed()
{
static if (is(typeof(this) : Tuple!A, A...))
alias RevTypes = Reverse!A;
Tuple!RevTypes result;
auto tup = this.tupleof; // can't call Reverse(this.tupleof) directly
result.tupleof = Reverse!tup;
return result;
}
Comment #1 by k.hara.pg — 2013-04-05T12:47:14Z
(In reply to comment #0)
> template Templ(T...) { alias Templ = T; }
>
> struct S { int x, y; }
>
> void main()
> {
> S s;
> auto tup = s.tupleof;
>
> auto x = Templ!(tup); // ok
> auto y = Templ!(s.tupleof); // ng
> }
This is disallowed by current language spec.
`s.tupleof` makes a tuple of expressions (s.x, s.y). But template cannot take runtime evaluated expressions. So it would be rejected.
> For a use-case, a recently created 'reverse' function for the Phobos Tuple type
> had this implementation:
>
> @property
> auto reversed()
> {
> static if (is(typeof(this) : Tuple!A, A...))
> alias RevTypes = Reverse!A;
>
> Tuple!RevTypes result;
> auto tup = this.tupleof; // can't call Reverse(this.tupleof) directly
> result.tupleof = Reverse!tup;
> return result;
> }
In member function, template can take field variables as symbol. So this works.
struct S {
int x;
string y;
auto test() {
auto t = Templ!(this.tupleof); // same as Templ!(x, y)
pragma(msg, typeof(t)); // (string, int)
}
}
Comment #2 by andrej.mitrovich — 2013-04-05T12:55:28Z
Yeah I guess it makes sense this would be disallowed, I wrongly interpreted 't.tupleof' to be a set of symbols.