www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19480] New: Take opSlice of string should work at least with

https://issues.dlang.org/show_bug.cgi?id=19480

          Issue ID: 19480
           Summary: Take opSlice of string should work at least with no
                    args
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: destructionator gmail.com

Autodecode strikes again! Consider the following:

import std.range;

void main() {
        auto s = "test".take(3);
        string str = s[];
}

This fails with s does not have operator []. Make it int and it works:

import std.range;

void main() {
        auto s = [1,2,3,4].take(3);
        int[] str = s[];
}


I propose that it should work for strings too. First, autodecode should be
killed wholly and permanently. But if that isn't going to happen, we can still
capture it all regardless:

If the original range hasLength and hasSlicing and is already of the char type,
when we ask for the whole thing, it is still a constant time function: there's
no need to decode characters in the middle because we know we want it all. It
is unambiguous if you want the nth code unit or the nth code point, since the
whole thing are the same in both cases anyway.


So I say we add a no-arg opSlice that just returns the whole view of the
underlying string, thus enabling zero-cost conversion back to string.


foreach(chunk; "foo".chunks(3))
   string s = chunk[];


would work, whereas now it doesn't and I don't think there even is a free way
to get it back to string type - suggestions like to!string are wasting computer
time.

--
Dec 12 2018