hasLength

Detects whether R has a length property.

R does not have to be a range to support the length.

Length mustn't be a $(D_KEYWORD @property) or a function, it can be a member variable or $(D_KEYWORD enum). But its type (or the type returned by the appropriate function) should be $(D_KEYWORD size_t), otherwise $(D_PSYMBOL hasLength) is $(D_KEYWORD false).

All dynamic arrays except $(D_KEYWORD void)-arrays have length.

enum bool hasLength(R);

Return Value

$(D_KEYWORD true) if R has a length property, $(D_KEYWORD false) otherwise.

Examples

static assert(hasLength!(char[]));
static assert(hasLength!(int[]));
static assert(hasLength!(const(int)[]));

struct A
{
    enum size_t length = 1;
}
static assert(hasLength!(A));

struct B
{
    @property size_t length() const @nogc nothrow pure @safe
    {
        return 0;
    }
}
static assert(hasLength!(B));

struct C
{
    @property const(size_t) length() const @nogc nothrow pure @safe
    {
        return 0;
    }
}
static assert(!hasLength!C);

See Also

$(D_PSYMBOL isInfinite).

Meta