Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take opSlice of string should work at least with no args #10363

Open
dlangBugzillaToGithub opened this issue Dec 12, 2018 · 0 comments
Open

Take opSlice of string should work at least with no args #10363

dlangBugzillaToGithub opened this issue Dec 12, 2018 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

destructionator (@adamdruppe) reported this on 2018-12-12T13:26:03Z

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

Description

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.
@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants