Variadic grouping

Artur Skawina art.08.09 at gmail.com
Sat Aug 10 04:54:22 PDT 2013


On 08/10/13 12:19, John Colvin wrote:
> On Monday, 29 July 2013 at 13:23:23 UTC, JS wrote:
>> Sometimes it's nice to be able to have groups of variadic parameters:
>>
>> template t(T1..., T2...)
>>
>> ...
>>
>> t!(a, b, c; d, e, f);
>>
>> so that a,b,c are for T1 and d,e,f are for T2.
>>
>> This can be done by making a symbol and breaking up a single variadic but is messy.
>>
>> I doubt such a feature will ever get added but who knows...
> 
> I was initially unimpressed, but after some recent work I would really like this feature. I have been using
>   struct Group(T...)
>   {
>       alias Ungroup = T;
>   }
> but, useful as it is, this feels like something that should have some sugar on it.
> 
>   template A(T0 ..., T1 ...)
>   {
>   // use T0 & T1
>   }
> 
>   A!(int, long, double; Point, int)
> 
> is so much nicer than
> 
>   template A(T0, T1)
>   if(isGroup!T0 && isGroup!T1)
>   {
>       alias t0 = T0.Ungroup;
>       alias t1 = T1.Ungroup;
> 
>       //use t0 && t1
>   }
> 
>   A!(Group!(int, long, double), Group!(Point, int))

   template Tuple(A...) { alias Tuple = A; }

   template Ungroup(G...) if (G.length==1) {
      static if(__traits(compiles, Tuple!(G[0].Ungroup)))
         alias Ungroup = Tuple!(G[0].Ungroup);
      else
         alias Ungroup = Tuple!(G[0]);
   }

then

   template A(T...) {
      //alias t0 = Ungroup!(T[0]);
      //alias t1 = Ungroup!(T[1]);
      
      //use t0 && t1

      static assert( {
         foreach (ET; T)
            pragma(msg, Ungroup!ET);
         return 1;
      }());
      
      alias A = Ungroup!(T[0])[0];
      Ungroup!(T[2])[$-1] AFloat;
   }

   A!(Group!(int, long, double), Group!(Point, int), float) x;
   A!(int, Group!(Point, int), Group!("f", float)) x2;
   A!(int, Group!(Point, int), float, "blah", double, Group!(ubyte, "m", float[8], 3.14)) x3;
   A!(int, 0, float, Group!(ubyte, "m", float[2], Group!(Group!(int,11,12), Group!(float,21,22)))) x4;


Using just ';' would: a) be too subtle and confusing; b) not be enough
to handle the 'x4' case above.

artur


More information about the Digitalmars-d mailing list