Re: phobos: What type to use instead of File when doing I/O on streams?
On Monday, 9 November 2015 at 19:38:06 UTC, Ali Çehreli wrote: As far as I understand, the issue is to prevent the seek() call at compile time, right? If so, the range types that byLine() and byLineCopy() return does not have such a member function. I think the code achieves that goal because we are not dealing with File objects anymore. Yes and no. Again, seek() was just an example. I'm looking for a replacement for the deprecated inferfaces InputStream and OutputStream.
Re: phobos: What type to use instead of File when doing I/O on streams?
On Monday, 9 November 2015 at 18:44:00 UTC, Alex Parrill wrote: Ranges are streams. file.byLine(Copy) and byChunk are effectively streams that are ranges. I might be wrong, but from what I read so far I don't think that "ranges are streams": - Can you read an arbitrary length of bytes from a range? (Reading by line in my code was just an example.) The only way I see is to make it a byte range. But then reading n bytes would result in n calls to popFront(), which is out of the question for performance reasons. Is this correct? - Can you flush() a range? - Can you use select() on a range? etc. (BTW. Where do I find select()/poll() in phobos?)
Re: phobos: What type to use instead of File when doing I/O on streams?
On Monday, 9 November 2015 at 14:48:35 UTC, Ali Çehreli wrote: import std.stdio; import std.range; void foo(I, O)(I in_stream, O out_stream) if (isInputRange!I && isOutputRange!(O, ElementType!I)) { // in_stream.seek(3); // compile error - good :) foreach (element; in_stream) { out_stream.put(element); } } void main(string[] args) { // Also consider .byLine, which is faster and risky foo(stdin.byLineCopy, stdout.lockingTextWriter); } Ali Uhm... no. That's not a solution to my problem. Ranges are not (I/O-)streams.
Re: phobos: What type to use instead of File when doing I/O on streams?
My question is now: How can I make this work without using deprecated stuff? import std.cstream; void foo(InputStream in_stream, OutputStream out_stream) { // in_stream.seek(3); // compile error - good :) char[] line; while ((line = in_stream.readLine()) !is null) out_stream.writeLine(line); } void main(string[] args) { foo(din, dout); }
Re: phobos: What type to use instead of File when doing I/O on streams?
On Sunday, 8 November 2015 at 23:50:58 UTC, Adam D. Ruppe wrote: You don't, in general. stdin is sometimes seekable and the compiler doesn't know if it is or not until you try at runtime. Hm. "Maybe the stream is seekable, maybe it is not" is not really an option for a language that is supposed to be type safe. I just found std.stream which looks very good. But it is deprecated?? You could write a wrapper struct for File though that @disables the seek function. It would have a File member, alias file this;, a constructor that forwards to it (optionally, you could also construct it as a File and assign it), and then a @disabled function with the same signature as the seek call in File. Then, it will no longer compile as long as you use your wrapper. Thank you for your solution, but this sounds more lika a hack.
Re: phobos: What type to use instead of File when doing I/O on streams?
On Sunday, 8 November 2015 at 21:57:55 UTC, Alex Parrill wrote: Post what you're trying to do if you want an example. $ cat test.d import std.stdio; //void foo(Stream in_stream, Stream out_stream) // I want something like this void foo(File in_stream, File out_stream) { in_stream.seek(3); // BUG string line; while ((line = in_stream.readln()) !is null) out_stream.write(line); } void main(string[] args) { foo(stdin, stdout); } $ cat test.txt Line 0 Line 1 Line 2 $ gdc -o test test.d $ ./test
phobos: What type to use instead of File when doing I/O on streams?
Hello again, I'm a bit puzzled by the "File" type. Looking at the implementation, it seems that all I/O functions were stuffed into a single class^H^H^H^H^Hstruct. What I expected to find is some kind of "Stream" class (with read(), write(), eof()), which is extended by a "File" class (with seek(), mmap(), etc.). So, assuming my program reads from stdin and is supposed to work on a file as well as on a pipe (which is not seekable) - how can I make the compiler bark when I accidently use stdin.seek()? Am I missing something here, too?
Re: foreach statement: Are there no Iterators in D?
On Sunday, 8 November 2015 at 11:47:41 UTC, Rikki Cattermole wrote: opApply if you want 0 .. N iterations during for a foreach statement and having it reset each time. No, that won't help. I want to be able to iterate over a data set of infinite size. Otherwise you want ranges :) An input range is more or less an iterator as you would think of it. You only need popFront, front and empty. Ah yes, that's what I missed. Looks good. Thank you. :)
foreach statement: Are there no Iterators in D?
Hello, I am looking for something like the "Iterator" Interface in Java or PHP. Is there something comparable in D that can be used with foreach? Thanks, J.Frank