www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13863] New: More readable template constraints


          Issue ID: 13863
           Summary: More readable template constraints
           Product: D
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: kiithsacmp gmail.com

Complicated template constraints in function signatures of many Phobos
functions severely affect readability of standard library documentation.

This could be mitigated by refactoring the constraints into separate template
predicates invoked in the (now much shorter) template constraints. These
predicated can then be descriptively documented as well.

E.g. instead of:

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]))) &&
== 1 || is(typeof(countUntil!pred(haystack, needles[1..$])))));

Use something like this:

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

/** 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...)

See also:
http://forum.dlang.org/post/mailman.3193.1418597982.9932.digitalmars-d puremagic.com
http://forum.dlang.org/post/slssjxbsguvhcbdlfefu forum.dlang.org

Dec 15 2014