<br><br><div class="gmail_quote">On Wed, Jul 10, 2013 at 6:16 PM, Jonathan M Davis <span dir="ltr"><<a href="mailto:jmdavisProg@gmx.com" target="_blank">jmdavisProg@gmx.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On Wednesday, July 10, 2013 18:10:42 Timothee Cour wrote:<br>
> On Wed, Jul 10, 2013 at 5:49 PM, Artur Skawina <<a href="mailto:art.08.09@gmail.com">art.08.09@gmail.com</a>> wrote:<br>
> > On 07/11/13 00:52, Timothee Cour wrote:<br>
> > > Why not support Tuple indexing and slicing with [] syntax?<br>
> > ><br>
> > > (see below for a way to index/slice a tuple)<br>
> ><br>
> > Not sure I understand the question.<br>
> > I guess you'd like this to work for some library pseudo-tuple type<br>
> > - I've not looked at those, somebody else may give you a better answer.<br>
> ><br>
> > "Native" tuples already support what you're asking for:<br>
> > template Tuple(A...) { alias Tuple = A; }<br>
> ><br>
> > void main(){<br>
> ><br>
> > alias T=Tuple!(int,double);<br>
> > pragma(msg,T[0].stringof);<br>
> > pragma(msg,T[0..2].stringof);<br>
> > pragma(msg,typeof(T.init[0]).stringof);<br>
> > pragma(msg,typeof(Tuple!(T.init[0..$])).stringof);<br>
> ><br>
> > }<br>
> ><br>
> > artur<br>
><br>
> I know native tuples do, I am aksing about std.typecons.Tuple.<br>
> Is there any way to support this in library code ?<br>
> If not, how about changing compiler to allow this?<br>
><br>
> I'd like that:<br>
> static assert(is(Tuple!(int,double)[0]==int));<br>
> static assert(is(Tuple!(int,double)[0..$]==Tuple!(int,double)));<br>
<br>
</div></div>Do you want to slice the _type_ or the object? Adding slicing of the value<br>
should be trivial enough if Tuple doesn't support it already. But slicing the<br>
type would require that the compiler understand Tuple or that some means of<br>
overloading opSlice on the types themselves (rather the objects of those<br>
types) be provided. And I wouldn't expect either of those to happen.<br>
<span class="HOEnZb"><font color="#888888"><br>
- Jonathan M Davis<br>
</font></span></blockquote></div><br><div><br></div><div>ok I found a great solution that allows lightweight slicing/indexing of std.typecons.Tuple:</div><div>using the Expand template below.</div><div><br></div><div>Can we add this to std.typetuple? or std.typecons?</div>
<div><br></div><div><div>import std.typecons;</div><div><br></div><div>template Expand(T)if(isTuple!T){</div><div>  alias Expand=typeof(T.init.expand);</div><div>}</div><div><br></div><div>unittest{</div><div>  import std.typetuple;</div>
<div>  alias T=Tuple!(int,double);</div><div>  static assert(is(Expand!T == TypeTuple!(int,double)));</div><div><br></div><div>  //now we can easily slice/index T:</div><div>  static assert(is(Expand!T[0..1] == TypeTuple!(int)));</div>
<div>  static assert(is(Expand!T[0] == int));</div><div>}</div><div><br></div></div><div><br></div>