"isDroppable" range trait for slicing to end

Jonathan M Davis jmdavisProg at gmx.com
Wed Oct 31 01:57:04 PDT 2012


On Wednesday, October 31, 2012 12:37:10 Dmitry Olshansky wrote:
> I just wanted to point out that we may as well require all RA ranges to
> have opDollar. Finite already have length, infinite would have marker.
> Just need some migration path so that current RA won't lose their title
> over night.

I agree, but for that to be realistic, I think that issue# 7177 needs to be 
implemented first. You should check out the pull request that I have for 
improving hasSlicing. I just updated it according to some of the discussion 
here, and it now checks the behavior of opDollar when it works with the range 
being sliced. For finite ranges, it essentially enforces that they function 
like arrays do, and for infinite ranges, it comes as close to that as it can:

https://github.com/D-Programming-Language/phobos/pull/854

As of the latest state of that pull request, hasSlicing looks like

template hasSlicing(R)
{
    enum bool hasSlicing = !isNarrowString!R && is(typeof(
    (inout int _dummy=0)
    {
        R r = void;

        static if(isInfinite!R)
            typeof(take(r, 1)) s = r[1 .. 2];
        else
            R s = r[1 .. 2];

        s = r[1 .. 2];

        static if(is(typeof(r[0 .. $])))
        {
            R t = r[0 .. $];
            t = r[0 .. $];

            static if(!isInfinite!R)
            {
                R u = r[0 .. $ - 1];
                u = r[0 .. $ - 1];
            }
        }

        static assert(isForwardRange!(typeof(r[1 .. 2])));
        static assert(hasLength!(typeof(r[1 .. 2])));
    }));
}


- Jonathn M Davis


More information about the Digitalmars-d mailing list