Deprecation: module std.stream is deprecated

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 10 03:56:51 PST 2015


On Sunday, November 08, 2015 14:41:05 Spacen Jasset via Digitalmars-d-learn wrote:
> This looks the simplest solution at the moment:
>
> >>     auto inputRange = File(__FILE__).byChunk(1024).joiner;
> >>     Foo foo = Foo(inputRange);
>
> But it doesn't seem efficient and strays off the conceptual path.
> In other words, why chunk things up, join them back, to get a
> stream? Perhaps the problem is that File is missing a .range()
> function?

It's actually doing what any "stream" based solution would have to do
anyway. If you're not reading the entire file in at once, then you're going
to be reading it in in chunks, even if that's wrapped up in some kind of
Stream class/struct. std.stdio.File exposes the basic operations and higher
level stuff can be done on top of them.

Maybe std.stdio.File should have some sort of function on it to just give
you a range of bytes or characters, but it's pretty trivial to use joiner to
get that, and it would definitely be worse to _not_ provide functions like
byLine or byChunk. Chaining functions like this is an incredibly common
thing to do with ranges. Range-based functions tend to be treated like
building blocks rather than kitchen sinks. And most range-based functions
are lazy, so something like

auto range = file.byChunk(1024).joiner();

is actually efficient, and if we did have a function on std.stdio.File to
just return a range of bytes or characters, it would almost certainly just
be wrapping something like this, since it is efficient, and it wouldn't make
sense to implement that functionality again rather than just use the
functions that we already have.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list