wrong isInputRange design

ag0aep6g via Digitalmars-d digitalmars-d at puremagic.com
Sun Dec 4 07:12:30 PST 2016


On Sunday, 4 December 2016 at 13:37:35 UTC, rumbu wrote:
> There is nowhere in the documentation where arrays are 
> advertised as  ranges by default.

Huh. It really doesn't seem to say so in the prose. It's shown in 
the examples for isInputRange, though:

     static assert( isInputRange!(int[]));
     static assert( isInputRange!(char[]));

> I think that the array range UFCSs must be moved out from the 
> std.range.primitives and let the library user to decide if 
> there is a need for range semantics applied to all arrays.

I'm afraid that's not so easy. std.range.primitives.isInputRange 
cannot see the range primitives when you import/define them 
yourself. You'd have to make isInputRange a mixin template, or 
replace the UFCS primitives with a wrapper type, or something 
like that. Either way, it'd be a major breaking change.

If you're passionate about this, I suggest you try and find a 
solution with an acceptable migration path. Work out all the 
annoying little details, and present it as a fleshed-out 
proposal. I wouldn't bet on it getting accepted, though. The 
current situation may be slightly irritating, but it may be the 
lesser evil when compared to breaking everyone's code by messing 
with arrays as ranges.

> Otherwise, as long as you want array specializations for your 
> functions, you must decorate all the range specializations with 
> (isInputRange!T && !isArray!T). And you are compelled to use 
> "f(T)(T t) if isArray!T" for all your array specializations 
> instead of "f(T)(T[] x)".

You can also use a "specialization" (in the narrower sense of the 
word that the spec uses):

     import std.range: isInputRange;
     import std.stdio: writeln;
     void f(R)(R r) if (isInputRange!R) { writeln("range"); }
     void f(A : E[], E)(A a) { writeln("array"); }
     void main() { int[] a; f(a); } /* calls the second overload; 
prints "array" */

See https://dlang.org/spec/template.html#parameters_specialization


More information about the Digitalmars-d mailing list