Overhauling the notion of output range
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sun Jul 11 18:17:44 PDT 2010
The notion of output range has been a tad vague in the past; up until
now a range that wanted to qualify as an output range had to define a
method called put.
That definition is awkward though. For example, the std.algorithm.copy()
primitive should work for an output range, but also for some other
ranges that allow assignment to front.
In related news, there's been this burning desire regarding a
lightweight output range defined as simply a delegate that accepts
const(char)[]. That range is an output range all right, and the way you
output to it is by simply calling the delegate!
All of the three mechanisms above are desirable for certain types. So
imagine this dialog (paraphrased from
http://www.youtube.com/watch?v=MrTsuvykUZk):
Guy 1: We need a good output range interface, and we have three good
candidates. We should make every one an output range.
Guy 2: What do you mean, every one?
Guy 1: E-V-E-R-Y O-N-E!!!!
So, I defined isOutputRange!R to yield true if at least one of these
conditions above is met: put() definition, input range with assignable
front, delegate.
Please refer to this code and this doc:
http://www.dsource.org/projects/phobos/browser/trunk/phobos/std/range.d#L227
http://erdani.com/d/phobos/std_range.html
This confers a ton of flexibility to programmers who need to define
output ranges. I've also reworked std.format to use put(r, e) so now it
works with all output ranges seamlessly.
Any thoughts would be appreciated. Thanks!
Andrei
More information about the Digitalmars-d
mailing list