是 RandomAccess 和 hasLength 但不是 hasSlicing 的 D 范围?

A D range that is RandomAccess and hasLength but not hasSlicing?

在 Phobos 的 findSplit 实现中,我们有 this special case:

static if (isSomeString!R1 && isSomeString!R2
        || (isRandomAccessRange!R1 && hasSlicing!R1 && hasLength!R1 && hasLength!R2))
{
    auto balance = find!pred(haystack, needle);
    immutable pos1 = haystack.length - balance.length;
    immutable pos2 = balance.empty ? pos1 : pos1 + needle.length;
    return Result!(typeof(haystack[0 .. pos1]),
                   typeof(haystack[pos2 .. haystack.length]))(haystack[0 .. pos1],
                                                              haystack[pos1 .. pos2],
                                                              haystack[pos2 .. haystack.length]);
}

这里的大部分限制都是有意义的。我知道我们需要一个随机访问的范围,并且 haystackneedle 都需要一个大小。但是 hasSlicing 检查让我感到惊讶。

我希望任何同时为 RandomAccesshasLength 的范围都能够支持 Slicing。尽管是 RandomAccesshasLength,但是否存在本质上无法支持 Slicing 的示例范围?

或者这更多的是用户可能提供的范围出于某种原因只是选择不实施该特定操作的问题?

我直接找到了源头,并在 Twitter 上询问了 Andrei Alexandrescu。 He responded:

I don't think there's an interesting case of a random access range without slicing (or vice versa). When we introduced ranges we wanted to be as general as possible, but that turned out to be overengineering.