Here is a sort of proof of concept:

struct CtsExtend
{
        static auto opCall(T)(T a)
        {

                struct _CtsExtend
                {
                        T _a;
                        auto opIndex(int i)
                        {
                                if (i < 0) return a[0];
                                if (i >= a.length) return a[$-1];
                                return a[i];
                        }
                }

                _CtsExtend x;
                x._a = a;

                return x;
        }
}


Not sure if it can be simplified(I had to create a sub struct to get things to work, hopefully it would be optimized out or can be simplified. I tried originally to do it all using meta programming but I couldn't figure it out).

For any indexable it will override the index and modify it's behavior to constantly extend the ends.

CtsExtend(arr)[-4]

In general it would be nice to get this type of thing full featured(the various extensions, for it to be optimized, to work with ranges and other types of indexables that might allow negative indices, override the extension values, keep a history, etc...

If it can be done and make to work well with ranges it would allow many algorithms to be very easily expressed and make ranges more powerful.

Reply via email to