Enum member hasLength

Detects whether R has a length property.

enum hasLength(R) = is(ReturnType!((R r) => r.length) == size_t);

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

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

All dynamic arrays except void-arrays have length.

Parameters

NameDescription
R A type.

Returns

true if R has a length property, false otherwise.

See Also

isInfinite.

Example

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);