Thread overview
[Issue 9886] New: Cannot pass .tupleof directly to a template
Apr 05, 2013
Andrej Mitrovic
Apr 05, 2013
Kenji Hara
Apr 05, 2013
Andrej Mitrovic
April 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9886

           Summary: Cannot pass .tupleof directly to a template
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-05 12:25:56 PDT ---
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;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9886



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2013-04-05 12:47:14 PDT ---
(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)
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9886


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-05 12:55:28 PDT ---
Yeah I guess it makes sense this would be disallowed, I wrongly interpreted 't.tupleof' to be a set of symbols.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------