Is this a bug in std.typecons.Tuple.slice?

Saurabh Das via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 4 21:18:01 PST 2016


On Thursday, 4 February 2016 at 17:52:16 UTC, Marco Leise wrote:
> https://issues.dlang.org/show_bug.cgi?id=15645

Is this a possible fixed implementation? :

         @property
         Tuple!(sliceSpecs!(from, to)) slice(size_t from, size_t 
to)() @trusted const
         if (from <= to && to <= Types.length)
         {
             auto sliceMixinGenerator()
             {
                 string rv;
                 for(auto i=from; i<to; ++i)
                 {
                     import std.conv : to;
                     rv ~= "returnValue[" ~ (i-from).to!string ~ 
"]=field[" ~ i.to!string ~"];";
                 }
                 return rv;
             }
             alias ReturnType = typeof(return);
             ReturnType returnValue;
             mixin(sliceMixinGenerator());
             return returnValue;
         }

         ///
         unittest
         {
             Tuple!(int, string, float, double) a;
             a[1] = "abc";
             a[2] = 4.5;
             auto s = a.slice!(1, 3);
             static assert(is(typeof(s) == Tuple!(string, float)));
             assert(s[0] == "abc" && s[1] == 4.5);

             Tuple!(int, int, long) b;
             b[1] = 42;
             b[2] = 101;
             auto t = b.slice!(1, 3);
             static assert(is(typeof(t) == Tuple!(int, long)));
             assert(t[0] == 42 && t[1] == 101);
         }

I'm unsure about:
1. Removing 'ref' from the return type
2. Adding 'const' to the function signature
3. Is the new implementation less efficient for correctly aligned 
tuples?



More information about the Digitalmars-d-learn mailing list