Enum std.range.primitives.member hasLength
Returns true
if R
has a length
member that returns an
integral type. R
does not have to be a range. Note that length
is an optional primitive as no range must implement it. Some
ranges do not store their length explicitly, some cannot compute it
without actually exhausting the range (e.g. socket streams), and some
other ranges may be infinite.
Although narrow string types (char[]
, wchar[]
, and their
qualified derivatives) do define a length
property,
yields hasLength
false
for them. This is because a narrow
string's length does not reflect the number of characters, but instead
the number of encoding units, and as such is not useful with
range-oriented algorithms.
Declaration
enum hasLength(R) = !isNarrowString!R && is(typeof((inout int = 0) { R r = R.init; ulong l = r.length; } ));
Example
static assert(!hasLength!(char[])); static assert( hasLength!(int[])); static assert( hasLength!(inout(int)[])); struct A { ulong length; } struct B { size_t length() { return 0; } } struct C { @property size_t length() { return 0; } } static assert( hasLength!(A)); static assert( hasLength!(B)); static assert( hasLength!(C)); } /** Returnstrue
ifR
is an infinite input range. An infinite input range is an input range that has a statically-defined enumerated member calledempty
that is alwaysfalse
, for example:
struct MyInfiniteRange
{
enum bool empty
= false;
...
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.