Surprising interaction of tuples and slicing

Ben Jones fake at fake.fake
Thu May 7 15:33:12 UTC 2020


I was doing some metaprogramming where I wanted to make a slice 
of a type:

alias Tbasic = int;
pragma(msg, Tbasic);
alias Tbasica = Tbasic[];
pragma(msg, Tbasica);

//prints int, int[]

And things worked fine until I attempted the same thing on what 
happened to be a tuple of 1 element:


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.

pragma(msg, Ts[0][]); //prints int[]

Is there any use for this behavior?  It seems like it might be 
worth warning like "slicing a tuple is a no-op"

Anyway, figured I'd post here for posterity since I was confused 
by it for a while.




More information about the Digitalmars-d-learn mailing list