www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13881] New: Optional length template argument for


          Issue ID: 13881
           Summary: Optional length template argument for
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

In some cases it can be useful to specify the length as template argument to
takeExactly, allowing the range to have a length known at compile-time:

void main() pure  safe nothrow  nogc {
    import std.range: takeExactly;
    int[10] arr;
    auto r = arr.takeExactly!5;
    enum len = r.length;

If the first argument of takeExactly is a fixed-size array of sufficient
length, or a range that has length known at compile-time (like another
takeExactly!N or an infinite range), then takeExactly becomes nothrow  nogc.

Now r.length is usable in various compile-time contexts:

    int[r.length] arr2 = void;
    copy(r, arr2[]);

If you can't add such optional template argument, then you can add a similar
function named std.range.takeExactlyCt.

See also Issue 13880

Dec 20 2014