std.algorithm.remove and principle of least astonishment

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Oct 16 14:31:29 PDT 2010


On 10/16/2010 02:56 PM, klickverbot wrote:
> On 10/16/10 9:47 PM, Andrei Alexandrescu wrote:
>> Thanks for the input. This is not a bug, it's what I believe to be a
>> very intentional feature: strings are not ordinary arrays because
>> characters have variable length. As such, assigning to "the first
>> character in a string" is not allowed because the assignment might mess
>> up the next character.
>
> I see that there is a problem due the difference of code units and code
> points, but why does the following work then?
>
> tmp = tmp[ 0 .. i ] ~ tmp[ ( i + 1 ) .. $ ];
>
> This is equivalent to my (naïve?) mental model of remove(), and thus it
> seems very counter-intuitive to me that one works, but the other doesn't.

To drive my point home: if you wanted to replace not 'x', but instead a 
multibyte character, your algorithm wouldn't work. It essentially 
assumes the string has one byte per character, and the needed cast to 
byte[] reflects that.

If anything, I'd call this a success.


Andrei


More information about the Digitalmars-d mailing list