Recommendations on avoiding range pipeline type hell

Adam D. Ruppe destructionator at gmail.com
Sat May 15 11:51:11 UTC 2021


On Saturday, 15 May 2021 at 11:25:10 UTC, Chris Piker wrote:
> Then the type definition of mega_range is something in the 
> order of:

The idea is you aren't supposed to care what the type is, just 
what attributes it has, e.g., can be indexed, or can be assigned, 
etc.

You'd want to do it all in one big statement, with a consumer at 
the end (and pray there's no errors cuz while you're supposed to 
hide from the type, it won't hide from you if there's a problem, 
and as you know the errors might be usable if they were formatted 
better and got to the point but they're not and sometimes the 
compiler withholds vital information from you! Error message 
quality is D's #1 practical problem. but ill stop ranting)

Anyway, you put it all in one bit thing and this is kinda 
important: avoid assigning it to anything. You'd ideally do all 
the work, from creation to conclusion, all in the big pipeline.

So say you want to write it

auto mega_range = range1.range2!(lambda2).range3!(lambda3);
writeln(mega_range);

that'd prolly work, writeln is itself flexible enough, but you'd 
prolly be better off doing like


```
range1
    .range2!lambda2
    .range3!lambda3
    .each!writeln; // tell it to write out each element
```

Or since writeln is itself a range consumer you could avoid that 
.each call. But it is really useful for getting the result out of 
a bit mess for a normal function that isn't a full range 
consumer. (It is basically foreach written as a function call 
instead of as a loop)


This way the concrete type never enters into things, it is all 
just a detail the compiler tracks to ensure the next consumer 
doesn't try to do things the previous step does not support.

> But, loops are bad.  On the D blog I've seen knowledgeable 
> people say all loops are bugs.

Meh, don't listen to that nonsense, just write what works for 
you. D's strength is that it adapts to different styles and meets 
you where you are. Listening to dogmatic sermons about idiomatic 
one true ways is throwing that strength away and likely to kill 
your personal productivity as you're fighting your instincts 
instead of making it work.


More information about the Digitalmars-d-learn mailing list