std.algorithm.remove and principle of least astonishment

Rainer Deyke rainerd at eldwood.com
Mon Nov 22 13:55:48 PST 2010


On 11/22/2010 11:55, Andrei Alexandrescu wrote:
> On 11/22/10 4:01 AM, Rainer Deyke wrote:
>> I see, foreach still iterates over code units by default.  Of course,
>> this means that foreach over ranges doesn't work with strings, which in
>> turn means that algorithms that use foreach over ranges are broken.
>> Observe:
>>
>>    import std.stdio;
>>    import std.algorithm;
>>
>>    void main() {
>>      writeln(count!("true")("日本語")); // Three characters.
>>    }
>>
>> Output (compiled with Digital Marse D Compiler v2.050):
>>    9
> 
> Thanks.
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=5257

I think this bug is a symptom of a larger issue.  The range abstraction
is too fragile.  If even you can't use the range abstraction correctly
(in the library that defines this abstraction no less), how can you
expect anyone else to do so?

At the very least, this is a sign that std.algorithm needs more thorough
testing, and/or a through code review.  This is far from the only use of
foreach on a range in std.algorithm.  It just happens to be the first
example I found to illustrate my point.


-- 
Rainer Deyke - rainerd at eldwood.com


More information about the Digitalmars-d mailing list