Bug 13881 – Optional length template argument for std.range.takeExactly
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-12-20T13:57:00Z
Last change time
2017-07-01T18:41:31Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-12-20T13:57:26Z
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
Comment #1 by yebblies — 2015-01-24T15:48:26Z
Range length can't be a compile-time constant, or it won't be able to support popFront and isn't a range.
Comment #2 by dlang-bugzilla — 2017-07-01T18:41:31Z
As Daniel said, a range with an immutable length might as well itself be immutable (not to be confused with a range of immutable elements). As such, it would be of very limited use. I don't see a point in pursuing this.