Bug 12470 – std.array.replace does not work with inout(char)[]

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-03-25T15:13:19Z
Last change time
2017-10-16T09:57:58Z
Assigned to
No Owner
Creator
Andrej Mitrovic

Comments

Comment #0 by andrej.mitrovich — 2014-03-25T15:13:19Z
----- import std.array; inout(char)[] sanitize(inout(char)[] input) { return input.replace("\0", " "); } ----- $ dmd test.d ----- C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\array.d(1835): Error: template std.array.replaceInto cannot deduce function from argument types !()(Appender!(inout(char)[]), inout(char)[], string, string), candidates are: C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\array.d(1844): std.array.replaceInto(E, Sink, R1, R2)(Sink sink, E[] subject, R1 from, R2 to) if (isOutputRange!(Sink, E) && isDynamicArray!(E[]) && isForwardRange!R1 && isForwardRange!R2 && (hasLength!R2 || isSomeString!R2)) test.d(7): Error: template instance std.array.replace!(inout(char), string, string) error instantiating ----- Note that this is an internal library error.
Comment #1 by acehreli — 2014-03-26T22:51:32Z
Still, that code cannot be compiled, right? inout is a wildcard that should be able to take place of const and immutable but std.array.replace cannot work with const(char)[] nor with immutable(char)[].
Comment #2 by andrej.mitrovich — 2014-03-27T04:12:05Z
(In reply to comment #1) > Still, that code cannot be compiled, right? > > inout is a wildcard that should be able to take place of const and immutable > but std.array.replace cannot work with const(char)[] nor with > immutable(char)[]. I thought it was supposed to allocate a new array to store the result to?
Comment #3 by monarchdodra — 2014-03-27T04:43:21Z
(In reply to comment #2) > (In reply to comment #1) > > Still, that code cannot be compiled, right? > > > > inout is a wildcard that should be able to take place of const and immutable > > but std.array.replace cannot work with const(char)[] nor with > > immutable(char)[]. > > I thought it was supposed to allocate a new array to store the result to? Seems that way: http://dlang.org/phobos/std_array.html#replace Replace occurrences of from with to in subject. Returns a new array without changing the contents of subject, or the original array if no match is found. /---- import std.array, std.stdio; void main() { int[] c = [1, 2, 3]; c.replace([2], [4, 4]).writeln(); //[1, 4, 4, 3]; c.writeln(); //[1, 2, 3]; c.idup.replace([2], [4, 4]).writeln(); //[1, 4, 4, 3]; } //---- Ali must be thinking about "replaceInPlace" ?
Comment #4 by acehreli — 2014-03-27T10:40:12Z
Sorry for the noise. :( Yes, I thought it was in-place.
Comment #5 by andrej.mitrovich — 2014-04-23T19:58:13Z
It seems the bottom-line problem is this: ----- import std.array; import std.range; inout(char)[] sanitize(inout(char)[] input) { auto app = appender!(inout(char)[])(); // fails because of inout(char) as the element type static assert(isOutputRange!(typeof(app), inout(char))); } ----- And it fails because of this strange nested declaration inside of the put method: @property ref E[] EArrayInit(); //@@@9186@@@: Can't use (E[]).init std\range.d(656): Error: inout on return means inout must be on a parameter as well for @property ref inout(char)[]() I guess this was some kind of funky workaround, but it causes issues for 12470. I'm not sure how to proceed yet.
Comment #6 by github-bugzilla — 2017-09-13T19:07:19Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/722ecd878cfde16a87d1f04e2ed3f5df08a9b78d fix issue 12470 - std.array.replace does not work with inout(char)[] https://github.com/dlang/phobos/commit/fc468500c90894919cec3783ce674f9b0f9b329e Merge pull request #5735 from BBasile/issue-12470 fix issue 12470 - std.array.replace does not work with inout(char)[]
Comment #7 by github-bugzilla — 2017-10-16T09:57:58Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/722ecd878cfde16a87d1f04e2ed3f5df08a9b78d fix issue 12470 - std.array.replace does not work with inout(char)[] https://github.com/dlang/phobos/commit/fc468500c90894919cec3783ce674f9b0f9b329e Merge pull request #5735 from BBasile/issue-12470