www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tuple indexing and slicing

reply Timothee Cour <thelastmammoth gmail.com> writes:
Why not support Tuple indexing and slicing with [] syntax?
 (see below for a way to index/slice a tuple)

void main(){
  alias T=Tuple!(int,double);
  pragma(msg,T[0].stringof);//_expand_field_0
  //pragma(msg,T[0..2].stringof); //Error: cannot slice type 'Tuple!(int,
double)
  pragma(msg,typeof(T.init[0]).stringof);//int
  pragma(msg,typeof(tuple(T.init[0..$])).stringof);//Tuple!(int,double)
}
Jul 10 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Timothee Cour:

 Why not support Tuple indexing and slicing with [] syntax?
You need a syntax like: a.slice!(1, 3); I don't know why Walter didn't add support for a slicing syntax for tuples. But it's an additive change, so it's not too much late to add it, if it's feasible. If D will grow built-in tuples (that currently is among my top enhancement requests for D), probably the slice syntax will be supported. Bye, bearophile
Jul 10 2013