Suggestion: array slice through arr[base, size]

Xinok via Digitalmars-d digitalmars-d at puremagic.com
Sun Feb 8 07:45:00 PST 2015


On Sunday, 8 February 2015 at 15:20:17 UTC, karl wrote:
> Hi, it's a bit unwieldy to write/read this:
> result = src[base .. base+size];
> instead of:
> result = src[base, size];
>
> Maybe such syntax would be a welcome addition to D? I don't see 
> it conflicting with the existing grammar, and only the 2 
> slicing-operators need further extending to support it.

If this is a common pattern in your code, you could write your 
own function to do this and call it using UFCS:

     Range slice(Range)(Range r, size_t base, size_t size)
     {
         return r[base .. base+size];
     }

     auto arr = [0,1,2,3,4,5,6,7,8,9];
     assert(arr.slice(4,2) == [4,5]);


More information about the Digitalmars-d mailing list