Why is std.algorithm so complicated to use?
Jonathan M Davis
jmdavisProg at gmx.com
Mon Jul 9 13:21:06 PDT 2012
On Monday, July 09, 2012 22:09:54 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
>
> Another example:
>
> auto str = ["foo", "bar"].map!(x => x);
> auto f = str.sort();
>
> Results in:
>
> http://pastebin.com/BeePWQk9
>
> I'm using DMD 2.059.
>From the looks of it (without digging into what's really going on), in both
cases, the problem seems to be that a template constraint is insufficiently
precise, and so it's attempting to instantiate a template when that
instantiation should fail. If a template constraint is written correctly, it
should be impossible for the template constraint to succeed and the template
instantiation fail.
In general, I think that std.range and std.algorithm need better unit tests
(ideally with every combination of range types that a particular function is
supposed to work with being tested for that function). There are definitely
cases where certain range types are supposed to work and don't (reference type
ranges being a prime example). I've started on that but haven't gotten all
that far yet. But with better testing, it should be much harder for a bad
template constraint to be missed.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list