Bug 19480 – Take opSlice of string should work at least with no args

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-12-12T13:26:03Z
Last change time
2024-12-01T16:34:39Z
Assigned to
No Owner
Creator
Adam D. Ruppe
Moved to GitHub: phobos#10363 →

Comments

Comment #0 by destructionator — 2018-12-12T13:26:03Z
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 by robert.schadek — 2024-12-01T16:34:39Z
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