Feedback on Streams concept, similar to Ranges
Andrew
andrewlalisofficial at gmail.com
Thu May 18 16:10:44 UTC 2023
On Thursday, 18 May 2023 at 01:31:21 UTC, Jacob Shtokolov wrote:
> 1. Have you looked at the
> [IOPipe](https://github.com/schveiguy/iopipe) library?
Yeah, I talked with schveiguy on the discord server for a bit; it
honestly looks like a better concept than what I'm doing, lol.
But I didn't notice it until yesterday. So maybe I should focus
my efforts there? I don't know yet.
> 2. What are the main benefits over the existing Ranges concept?
> Say, given your example:
>
> ```d
> import streams;
>
> void readFileTo(S)(string filename, S stream) if
> (isOutputStream!(S, ubyte)) {
> import std.stdio;
> auto fIn = FileInputStream(filename);
> transferTo(fIn, stream);
> }
> ```
>
> With ranges, this would look something like:
>
> ```d
> import std;
>
> void readFileTo(S)(string filename, ref S stream) if
> (isOutputRange!(S, ubyte[])) {
> File(filename).byChunk().copy(stream);
> }
> ```
>
> Which is more or less the same.
I would say that there isn't really a big benefit over using
ranges in terms of how they're expressed, but more that I think
(and I may be wrong) that streams are a simpler, easier concept
for programmers to grasp, especially those that are migrating to
D from some other language that uses a similar stream concept.
Another benefit is that, as pointed out by Guillame Pilot in
another thread, phobos ranges (and most of phobos for that
matter) has no real convention for naming schemes, and they
generally don't try to be betterC-compatible, so it makes it
difficult to use them in any low-level code.
Finally, my streams allow the code to handle errors more
gracefully without needing exceptions, which isn't always
convenient to do with ranges.
But you're right; to the average D programmer, streams are just a
different flavor of accomplishing the same thing.
> 3. In the README you write:
>
> ```
> Phobos' concept of an Input Range relies on implicit buffering
> of results... This doesn't map as easily to many low-level
> resources
> ```
>
> AFAIK, the read/write buffers are anywhere, except, probably,
> `sendfile()` and some combination of `mmap` and `write`. But
> I'm struggling to get how this streams concept maps onto
> `sendfile` as well.
Yeah, I suppose what I was trying to say, is that this library
puts the programmer in more control of if and when buffers are
allocated and used with IO. But of course for `sendfile` there's
no need for streams.
More information about the Digitalmars-d
mailing list