Thread overview
Re: Tuple indexing and slicing
Jul 11, 2013
Artur Skawina
Jul 11, 2013
Timothee Cour
Jul 11, 2013
Jonathan M Davis
Jul 11, 2013
Timothee Cour
Jul 11, 2013
Timothee Cour
July 11, 2013
On 07/11/13 00:52, Timothee Cour wrote:
> Why not support Tuple indexing and slicing with [] syntax?
>  (see below for a way to index/slice a tuple)

Not sure I understand the question.
I guess you'd like this to work for some library pseudo-tuple type
- I've not looked at those, somebody else may give you a better answer.

"Native" tuples already support what you're asking for:

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

   void main(){
     alias T=Tuple!(int,double);
     pragma(msg,T[0].stringof);
     pragma(msg,T[0..2].stringof);
     pragma(msg,typeof(T.init[0]).stringof);
     pragma(msg,typeof(Tuple!(T.init[0..$])).stringof);
   }

artur
July 11, 2013
On Wed, Jul 10, 2013 at 5:49 PM, Artur Skawina <art.08.09@gmail.com> wrote:

> On 07/11/13 00:52, Timothee Cour wrote:
> > Why not support Tuple indexing and slicing with [] syntax?
> >  (see below for a way to index/slice a tuple)
>
> Not sure I understand the question.
> I guess you'd like this to work for some library pseudo-tuple type
> - I've not looked at those, somebody else may give you a better answer.
>
> "Native" tuples already support what you're asking for:
>
>    template Tuple(A...) { alias Tuple = A; }
>
>    void main(){
>      alias T=Tuple!(int,double);
>      pragma(msg,T[0].stringof);
>      pragma(msg,T[0..2].stringof);
>      pragma(msg,typeof(T.init[0]).stringof);
>      pragma(msg,typeof(Tuple!(T.init[0..$])).stringof);
>    }
>
> artur
>

I know native tuples do, I am aksing about std.typecons.Tuple.
Is there any way to support this in library code ?
If not, how about changing compiler to allow this?

I'd like that:
static assert(is(Tuple!(int,double)[0]==int));
static assert(is(Tuple!(int,double)[0..$]==Tuple!(int,double)));


July 11, 2013
On Wednesday, July 10, 2013 18:10:42 Timothee Cour wrote:
> On Wed, Jul 10, 2013 at 5:49 PM, Artur Skawina <art.08.09@gmail.com> wrote:
> > On 07/11/13 00:52, Timothee Cour wrote:
> > > Why not support Tuple indexing and slicing with [] syntax?
> > > 
> > > (see below for a way to index/slice a tuple)
> > 
> > Not sure I understand the question.
> > I guess you'd like this to work for some library pseudo-tuple type
> > - I've not looked at those, somebody else may give you a better answer.
> > 
> > "Native" tuples already support what you're asking for:
> > template Tuple(A...) { alias Tuple = A; }
> > 
> > void main(){
> > 
> > alias T=Tuple!(int,double);
> > pragma(msg,T[0].stringof);
> > pragma(msg,T[0..2].stringof);
> > pragma(msg,typeof(T.init[0]).stringof);
> > pragma(msg,typeof(Tuple!(T.init[0..$])).stringof);
> > 
> > }
> > 
> > artur
> 
> I know native tuples do, I am aksing about std.typecons.Tuple.
> Is there any way to support this in library code ?
> If not, how about changing compiler to allow this?
> 
> I'd like that:
> static assert(is(Tuple!(int,double)[0]==int));
> static assert(is(Tuple!(int,double)[0..$]==Tuple!(int,double)));

Do you want to slice the _type_ or the object? Adding slicing of the value should be trivial enough if Tuple doesn't support it already. But slicing the type would require that the compiler understand Tuple or that some means of overloading opSlice on the types themselves (rather the objects of those types) be provided. And I wouldn't expect either of those to happen.

- Jonathan M Davis
July 11, 2013
On Wed, Jul 10, 2013 at 6:16 PM, Jonathan M Davis <jmdavisProg@gmx.com>wrote:

> On Wednesday, July 10, 2013 18:10:42 Timothee Cour wrote:
> > On Wed, Jul 10, 2013 at 5:49 PM, Artur Skawina <art.08.09@gmail.com>
> wrote:
> > > On 07/11/13 00:52, Timothee Cour wrote:
> > > > Why not support Tuple indexing and slicing with [] syntax?
> > > >
> > > > (see below for a way to index/slice a tuple)
> > >
> > > Not sure I understand the question.
> > > I guess you'd like this to work for some library pseudo-tuple type
> > > - I've not looked at those, somebody else may give you a better answer.
> > >
> > > "Native" tuples already support what you're asking for:
> > > template Tuple(A...) { alias Tuple = A; }
> > >
> > > void main(){
> > >
> > > alias T=Tuple!(int,double);
> > > pragma(msg,T[0].stringof);
> > > pragma(msg,T[0..2].stringof);
> > > pragma(msg,typeof(T.init[0]).stringof);
> > > pragma(msg,typeof(Tuple!(T.init[0..$])).stringof);
> > >
> > > }
> > >
> > > artur
> >
> > I know native tuples do, I am aksing about std.typecons.Tuple.
> > Is there any way to support this in library code ?
> > If not, how about changing compiler to allow this?
> >
> > I'd like that:
> > static assert(is(Tuple!(int,double)[0]==int));
> > static assert(is(Tuple!(int,double)[0..$]==Tuple!(int,double)));
>
> Do you want to slice the _type_ or the object? Adding slicing of the value
> should be trivial enough if Tuple doesn't support it already. But slicing
> the
> type would require that the compiler understand Tuple or that some means of
> overloading opSlice on the types themselves (rather the objects of those
> types) be provided. And I wouldn't expect either of those to happen.
>
> - Jonathan M Davis
>


ok I found a great solution that allows lightweight slicing/indexing of
std.typecons.Tuple:
using the Expand template below.

Can we add this to std.typetuple? or std.typecons?

import std.typecons;

template Expand(T)if(isTuple!T){
  alias Expand=typeof(T.init.expand);
}

unittest{
  import std.typetuple;
  alias T=Tuple!(int,double);
  static assert(is(Expand!T == TypeTuple!(int,double)));

  //now we can easily slice/index T:
  static assert(is(Expand!T[0..1] == TypeTuple!(int)));
  static assert(is(Expand!T[0] == int));
}


July 11, 2013
On Thu, Jul 11, 2013 at 1:05 AM, Simen Kjaeraas <simen.kjaras@gmail.com>wrote:

> On 2013-07-11, 00:52, Timothee Cour wrote:
>
>  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)
>> }
>>
>
> This works:
>
> import std.typecons : Tuple, tuple;
> void main() {
>     alias T = Tuple!(int, string, float);
>     T a;
>     Tuple!(int, string) b = tuple(a[0..2]); // Slicing values.
>     Tuple!(T.Types[1..3]) c; // Slicing types.
> }
>
> --
> Simen
>


ah, yes, I forgot about Types, thanks! Documentation for it could be improved though...