string to thread

Jonathan M Davis jmdavisProg at gmx.com
Sun Sep 9 05:26:14 PDT 2012


On Sunday, September 09, 2012 14:13:26 monarch_dodra wrote:
> Okay, that makes a sense to me. In c++, the paradigm is:
> *Streams for formating.
> *Iterators for algorithms.
> 
> Two paradigms => object to go from string(pointer/iterator) to
> stream.
> 
> And you are telling me that in D, everything is ranges, so there
> is no such need for a string stream. Everything operates on
> ranges, and you can (or should be able) to scare an range out of
> a stream, is this correct?

Pretty much. Some functions will never operate on ranges, and in some cases it 
may make sense to have a way to do something with ranges and a way to do it 
without them, but in general, things are going to be range based, and we may 
not even _have_ streams, since a stream is effectively a range.

> What bothers me though (and is the source of my confusion) is
> that in that case, shouldn't stdin and stdout also be ranges? Or
> at least, shouldn't there be a global equivalent symbol that is a
> range?

More likely, there will be a way to get a range over stdin or stdout. That's 
pretty much how we deal with it now (stdin and stdout are Files and get 
whatever functions File has). It's just that we need a better range type on 
Files (something which is forward range of bytes or characters rather than 
ranges of lines or chunks). That'll come with std.io, but Steven has been too 
busy to finish it yet.

> Instead of using std[in|out], I create an InputRange (a string),
> and an output range (an Appender!string). However, the methods
> readf/writefln have been changed!

I'd point out that formattedRead and formattedWrite aren't really meant to be 
used directly in your typical program, so they're not a terribly great 
example. Regardless, _some_ functions will make more sense to _not_ be range-
based, and even if something works better if it's range-based, some older stuff 
which was created before we started using ranges is going to stick around for 
backwards compatibility if nothing else. So, some things may get range-based 
replacements but still have the old versions around. Exactly what happens with 
input and output will depend heavily on what happens with std.io.

> This is what is confusing me: I have been taught to work on
> streams, but I can't use that interface on strings/ranges :/

I don't understand this remark. You mean that the ranges for operating on 
input and output properly don't exist yet? That's definitely true, but it will 
be fixed. If you mean something else, I don't know what you mean. Streams 
pretty much _are_ ranges.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list