Why is std.algorithm so complicated to use?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Jul 10 06:28:51 PDT 2012


On 7/10/12 2:50 AM, Jacob Carlborg wrote:
> On 2012-07-09 22:16, Andrei Alexandrescu wrote:
>
>> 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.
>
> Is that by design or something that can be fixed?

We can arrange things in the library that a custom message is issued, or 
in the compiler to do it once for all. At any rate whenever there's an 
error pointing somewhere in the library's code that's an insufficient 
template constraint that should be fixed.

>> To fix the example, write
>>
>> auto bar = foo.chain(["bar"]);
>
> I think that's an ugly workaround.

Well I made minimal changes. Besides I don't know what the intent is.

>>> 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
>
> "Clear as it goes" WTF? Are you nuts?

To the extent any of us is somewhat nuts, yes, I am. I'm also a fan of 
civil conversation.

> It's an insanly bad error message,
> I have no "r" in my code. Is it too much to ask to be able to sort a range?

Indeed I agree there should be no error in library code. What I meant to 
say was, when I saw the code I thought "I bet this is an lvalue thing", 
and then when I saw lvalue in the error I was satisfied.

> This just proves that std.algorithm is complicated to use. It's very
> unintuitive.
>
> What I really want is this, but ranges doesn't work like that:
>
> Foo f = Foo();
> Foo[] foos = [f];
> string[] = foos.map!(x => "foo");
> string[] bar = foo ~= "foo";

I understand. So you need to use array() to convert the lazy map result 
into an eager array. I disagree this is unintuitive, if it were then 
very little of D would make sense are lazy, non-array ranges are everywhere.

> And:
>
> string[] str = ["foo", "bar"].map!(x => x);
> string[] f = str.sort();

Same here. map() does not return arrays, and for very good reasons.


Andrei


More information about the Digitalmars-d mailing list