Output range primitives

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 5 14:28:31 PDT 2017


On Wednesday, July 05, 2017 21:13:57 Joel Nilsson via Digitalmars-d wrote:
> For something to be an output range it has to define the put
> primitive. I am wondering why there is only a single type of
> output range. Would it not make sense to have, say, a fillable
> output range which defines @property bool full()?
>
> This could be used in functions like copy that currently just
> spam the output range until there's nothing left to throw at it.
> I can imagine situations where you would want to continuously
> copy from the input range until the output range was "satisfied".
>
> Let's say we have a range that acts as a message queue between
> two threads. The consumer would have no issue checking if there
> are any available messages by calling empty, but the producer
> currently has no standard way of checking if the buffer is full.
>
> Is there any reason that this isn't in the range spec, or has the
> need for it not arised yet? Maybe it's just a poor idea?
> Enlighten me.

The reality of the matter is that output ranges have never been well fleshed
out. They seem to have been mainly created in order to have the opposite of
input ranges, but they were never designed anywhere near as thoroughly as
input ranges.

For some stuff to work well, we really do need some sort of property
analogous to length which indicates how many more elements could be put into
the output range, and having something like full that was analagous to empty
would make sense as well. As it stands, you basically get to put elements
into a range until it throws, because it's full, and while that makes sense
under some circumstances, in others, it is most definitely not ideal.

Also, std.digest introduced a primitive called finish (if I remember the
name correctly) which basically "finishes" an output range so that it's
possible for an algorithm to do something to it after it's been filled (e.g.
if you had to do a checksum on the data and add it to the end or something).
But while I believe that std.digest uses it consistently, it's never been
formalized beyond that.

There may be other things that should really be done to output ranges as
well, but really, what it comes down to is that they were designed just
enough to get by and then were never properly revisited. So, they definitely
work, but they're a bit hampered in comparison to what they could or should
be.

- Jonathan M Davis



More information about the Digitalmars-d mailing list