Why is std.algorithm so complicated to use?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Jul 9 13:16:42 PDT 2012


On 7/9/12 4:09 PM, Jacob Carlborg wrote:
> Almost every time I'm trying to use std.algorithm I run into some kind
> of error, for what seems to be fairly trivial and what one would expect
> to work. It feels like I'm constantly fighting with std.algorithm. For
> example:
>
> import std.algorithm;
> import std.range;
>
> struct Foo {}
>
> auto f = Foo();
> auto foos = [f];
> auto foo = foos.map!(x => "foo");
> auto bar = foo.chain("bar");
>
> This simple example result in the follow error:
>
> http://pastebin.com/E4LV2UBE

So foo is a range of strings, because each element of it is a string. 
Then you want to chain a range of strings with a string, which is a 
range of dchar. That doesn't work, and I agree the error message should 
be more informative.

To fix the example, write

auto bar = foo.chain(["bar"]);

> Another example:
>
> auto str = ["foo", "bar"].map!(x => x);
> auto f = str.sort();
>
> Results in:
>
> http://pastebin.com/BeePWQk9

The first error message is at clear as it goes:

Error: r[i2] is not an lvalue


Andrei



More information about the Digitalmars-d mailing list