[Issue 9886] Cannot pass .tupleof directly to a template

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Apr 5 12:47:15 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9886



--- Comment #1 from Kenji Hara <k.hara.pg at 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: -------


More information about the Digitalmars-d-bugs mailing list