Is there a convenicene function for assigning an empty dynamic array of a given type to a variable?

    cast(int[])[];

I'm using this in ForwardDifference constructor:

auto forwardDifference(R)(R r) if (isInputRange!R)
{
    import std.range: front, empty, popFront, dropOne;

    struct ForwardDifference
    {
        R _range;
alias E = ElementType!R; // Input ElementType alias D = typeof(_range.front - _range.front); // Element Difference Type. TODO: Use this as ElementType of range
        D _front;
        bool _initialized = false;

        this(R range)
        in { assert(!range.empty); }
        body {
            auto tmp = range;
if (tmp.dropOne.empty) // TODO: This may be an unneccesary cost but is practical to remove extra logic static if (isArray!R) // TODO: Construct R in a generic way that include dynamic arrays?
                    _range = cast(D[])[];
                else
                    _range = R(); // return empty range
            else
_range = range; // store range internally (by reference)
        }

        @property:
        auto ref front() {
            if (!_initialized) { popFront(); }
            return _front;
        }
        auto ref moveFront() {
            popFront();
            return _front;
        }
        void popFront() {
            if (empty is false) {
                _initialized = true;
                E rf = _range.front;
                _range.popFront();
                if (_range.empty is false)
                {
                    _front = _range.front - rf;
                }
            }
        }
        bool empty() {
            return _range.empty;
        }
    }

    return ForwardDifference(r);
}

Reply via email to