std.algorithm.remove and principle of least astonishment

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Oct 16 14:30:18 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.

Strings are dual types. They have [] and .length but not with the 
semantics required by ranges. So formally they don't support 
isRandomAccessRange and hasLength.

Andrei


More information about the Digitalmars-d mailing list