Lost a new commercial user this week :(

Andrej Mitrovic via Digitalmars-d digitalmars-d at puremagic.com
Sun Dec 14 14:59:33 PST 2014


On 12/14/14, Kiith-Sa via Digitalmars-d <digitalmars-d at puremagic.com> wrote:
> Example:
>
> ptrdiff_t countUntil(alias pred = "a == b", R, Rs...)(R haystack,
> Rs needles) if (isForwardRange!R && Rs.length > 0 &&
> isForwardRange!(Rs[0]) == isInputRange!(Rs[0]) &&
> is(typeof(startsWith!pred(haystack, needles[0]))) && (Rs.length
> == 1 || is(typeof(countUntil!pred(haystack, needles[1..$])))));
> ptrdiff_t countUntil(alias pred = "a == b", R, N)(R haystack, N
> needle) if (isInputRange!R &&
> is(typeof(binaryFun!pred(haystack.front, needle)) : bool));

Yeah, this is still a problem even if you're experienced with
templates. It's a wall of text.

It seems like we're treating template constraints as an internal
feature (just to use them to limit the possible arguments or allow
overloading against other templates), but I think constraints should
also be looked at as a source of documentation ("Can I call this
template with this argument type? Let's see the constraint.. Whoa this
is complicated..").

CSS trickery might work, but I'd prefer if we didn't beat around the
bush and instead implemented constraits as separate templates if they
become too complicated, e.g. turn the above to:

ptrdiff_t countUntil(alias pred = "a == b", R, Rs...)(R haystack,
Rs needles) if (canCountUntil!(pred, R, Rs);

And then the canCountUntil template would be separately documented,
something like:

/** A range can be counted if:
- R is a forward range
- pred is a valid string comparison predicate, or a function that can
compare R.front with Rs.front
- ...
 */
template canCountUntil(alias pred, R, Rs...)
{
    ...
}

I mean the predicate itself kind of documents this, but it's very
unreadable despite the logic making sense.

We already have things like isInputRange, and functions with
constraints would likely look horrible if we copy-pasted the
isInputRange implementation directly into their constraints rather
than use a simple isInputRange!R check. In the same fashion the
countUntil (and other functions with complex constraints) should have
a helper template that's used as a constraint (like I've shown above).


More information about the Digitalmars-d mailing list