Issue 19480 - Take opSlice of string should work at least with no args
Summary: Take opSlice of string should work at least with no args
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-12-12 13:26 UTC by Adam D. Ruppe
Modified: 2024-12-01 16:34 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adam D. Ruppe 2018-12-12 13:26:03 UTC
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.
Comment 1 dlangBugzillaToGithub 2024-12-01 16:34:39 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10363

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB