[Issue 10959] New: std.algorithm.remove is highly bug-prone

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 3 14:47:29 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10959

           Summary: std.algorithm.remove is highly bug-prone
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2013-09-03 14:47:27 PDT ---
This is not an enhancement request, I consider this a bug report.


Often when I use std.algorithm.remove in my code I introduce bugs. So I believe
std.algorithm.remove is too much bug-prone, some examples:

import std.algorithm: remove;
import std.stdio: writeln;
void main() {
    auto A = [1, 2, 3];
    A.remove(2);
    writeln(A); // prints: [1, 2, 3]
    A.remove!q{a = 2};
    writeln(A); // prints: [1, 2, 3]
    A.remove!q{a == 2};
    writeln(A); // prints: [1, 3, 3]
    A = [1, 2, 3];
    A = A.remove!q{a == 2};
    writeln(A); // prints: [1, 3] (correct)
}


So I suggest to rename std.algorithm.remove as "removeAtIndex" or something
similar. And then to introduce a function remove that removes the given item.
But this is not enough, because even this syntax is bug-prone to remove the
item '2' from the array 'A':

A = A.remove(2);

What I should write is just:

A.remove(2);

This is how it's done in Python, and it's not bug-prone, this is how in my
opinion it should be designed:

>>> A = [1, 2, 3]
>>> A.remove(2)
>>> A
[1, 3]


I don't care about all the discussions about containers, ranges, etc. Currently
std.algorithm.remove is a landmine and in my opinion it's not acceptable in
Phobos.

See also a thread by Manu and friends, that have had problems to remove an
array item:

http://forum.dlang.org/thread/mailman.680.1378001151.1719.digitalmars-d@puremagic.com

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list