Output range and writeln style functions
Jon Degenhardt via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jan 23 12:48:08 PST 2017
On Monday, 23 January 2017 at 08:03:14 UTC, Ali Çehreli wrote:
> On 01/22/2017 01:54 PM, Jon Degenhardt wrote:
>> I've been increasingly using output ranges in my code (the
>> "component
>> programming" model described in several articles on the D
>> site). It
>> works very well, except that it would often be more convenient
>> to use
>> writeln style functions rather than 'put'. Especially when you
>> start by
>> drafting a sketch of code using writeln functions, then
>> convert it an
>> output range.
>>
>> Seems an obvious thing, I'm wondering if I missed something.
>> Are there
>> ways to use writeln style functions with output ranges?
>>
>> --Jon
>
> I don't think I understand the question. :)
>
> If you need a variadic put(), then I've come up with the
> following mildly tested AllAppender. Just as a reminder, I've
> also used std.range.tee that allows tapping into the stream to
> see what's flying through:
>
> [snip]
>
> Ali
So I guess the is answer is "no" :)
It's mainly about consistency of the output primitives. Includes
variadic args, formatting, and names of the primitives. I keep
finding myself starting with something like:
void writeLuckyNumber(string name, int luckyNumber)
{
writefln("Hello %s, your lucky number is %d", name,
luckyNumber);
}
and then re-factoring it as:
void writeLuckyNumber(OutputRange)
(OutputRange outputStream, string name, int luckyNumber)
if (isOutputRange!(OutputRange, char))
{
import std.format;
outputStream.put(
format("Hello %s, your lucky number is %d\n", name,
luckyNumber));
}
Not bad, but the actual output statements are a bit harder to
read, especially if people reading your code are not familiar
with output ranges. So, what I'm really wondering is if there is
built-in way to get closer to:
outputStream.writefln(...);
that I've overlooked.
--Jon
More information about the Digitalmars-d-learn
mailing list