Surprising interaction of tuples and slicing

Ali Çehreli acehreli at yahoo.com
Thu May 7 23:07:40 UTC 2020


On 5/7/20 8:33 AM, Ben Jones wrote:> I was doing some metaprogramming 
where I wanted to make a slice of a type:

[...]

 > alias Ts = AliasSeq!int;
 > pragma(msg, Ts);
 > alias Tsa = Ts[];
 > pragma(msg, Tsa);
 > //prints (int), (int)
 >
 > which confused me until I realized that the [] was slicing the tuple.
 > Note, you can add as many [] as you want since it's basically a no-op.

[...]

 > Is there any use for this behavior?

Slicing an alias sequence is definitely useful e.g. in recursive templates:

   alias car = T[0];      // (head)
   alias cdr = T[1..$];   // (tail)

 > It seems like it might be worth
 > warning like "slicing a tuple is a no-op"

The trouble seems to be when slicing the entire tuple. Even in that 
case, printing a warning would not be desired in some situations 
ironically in generic code where e.g. T[0..$] may appear, which is the 
same as T[].

Ali



More information about the Digitalmars-d-learn mailing list