Can't use std.algorithm.remove on a char[]?

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 30 11:32:32 PDT 2016


On 30.04.2016 18:44, TheGag96 wrote:
> I was just writing some code trying to remove a value from a character
> array, but the compiler complained "No overload matches for remove", and
> if I specifically say use std.algorithm.remove() the compiler doesn't
> think it fits any definition. For reference, this would be all I'm doing:
>
> char[] thing = ['a', 'b', 'c'];
> thing = thing.remove(1);
>
> Is this a bug? std.algorithm claims remove() works on any forward range...

The documentation is wrong.

1) remove requires a bidirectional range. The constraints and parameter 
documentation correctly say so. char[] is a bidirectional range, though.

2) remove requires lvalue elements. char[] fails this, as the range 
primitives decode the chars on-the-fly to dchars.

Pull request to fix the documentation:
https://github.com/dlang/phobos/pull/4271

By the way, I think requiring lvalues is too restrictive. It should work 
with assignable elements. Also, it has apparently been missed that 
const/immutable can make non-assignable lvalues.

You can use std.utf.byCodeUnit to get a char range over an char[], but 
using it here is a bit awkward, because there's no (documented) way to 
get the array back from a byCodeUnit range:
----
char[] thing = ['a', 'b', 'c'];
thing = thing[0 .. thing.byCodeUnit.remove(1).length];
----

You could also use ubyte[] instead of char[]:
----
ubyte[] thing = ['a', 'b', 'c'];
thing = thing.remove(1);
----


More information about the Digitalmars-d-learn mailing list