finish function for output ranges
Dmitry Olshansky
dmitry.olsh at gmail.com
Sun Aug 12 04:23:57 PDT 2012
On 12-Aug-12 03:29, Andrei Alexandrescu wrote:
> N.B. I haven't yet reviewed the proposal.
>
> There's been a lot of discussion about the behavior of hash
> accumulators, and I've just have a chat with Walter about such.
>
> There are two angles in the discussion:
>
> 1. One is, the hash accumulator should work as an operand in an
> accumulation expression. Then the reduce() algorithm can be used as
> follows:
>
> HashAccumulator ha;
> reduce!((a, b) => a + b)(ha, [1, 2, 3]);
> writeln(ha.finish());
>
> This assumes the hash overloads operator +.
Would have been nice to have operator '<-' for "place" *LOL* :)
(OT: I think C++ would be a much better place if it had it for e.g.
iostream ...)
>
> I think we should reify the notion of finish() as an optional method for
> output ranges. We define in std.range a free finish(r) function that
> does nothing if r does not define a finish() method, and invokes the
> method if r does define it.
>
> Then people can call r.finish() for all output ranges no problem.
>
> For files, finish() should close the file (or at least flush it -
> unclear on that).
Easy to check:
{
File f = File("myfile", "w");
auto sink = f.lockingTextWriter;
dumpTo(sink, some_vars);
dumpTo(sink, some_other_vars);
sink.finish(); //would be taking on f's job to close file
return f; //and now what? clearly f is the one responsible
// (with the means to transfer that responsibility)
}
So IMO ranges should not step down to topology & origins of data be it
output range or input range. This also means that with streams, finish
is a flush and thus I'd expect finish to be callable many times in row.
>
> Destroy!
One thing I don't like about it is a by-hand nature of it. Manual way is
good only when you are interested in the result of finish.
I half expect to see rule #X of D coding standard:
use RAI or scope(exit) to flush an output range
--
Dmitry Olshansky
More information about the Digitalmars-d
mailing list