Bug 2081 – Foreach over Stream appears broken

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2008-05-08T16:58:00Z
Last change time
2014-04-23T18:31:29Z
Assigned to
nobody
Creator
jlquinn

Comments

Comment #0 by jlquinn — 2008-05-08T16:58:26Z
I'm trying to use the foreach idiom shown in the docs for InputStream, but the compiler rejects my code. The program is: import std.cstream; import std.stream; void test() { string file = "test"; Stream f = new BufferedFile(file); foreach (ulong line, string buf; f) { derr.writefln(buf); } } The compiler complains: dmd/bin/dmd junk2.d junk2.d(8): function std.stream.Stream.opApply (int delegate(ref char[] line)) does not match parameter types (int delegate(ref ulong __applyArg0, ref invariant(char)[] __applyArg1)) junk2.d(8): Error: cannot implicitly convert expression (__foreachbody15) of type int delegate(ref ulong __applyArg0, ref invariant(char)[] __applyArg1) to int delegate(ref ulong n, ref wchar[] line)
Comment #1 by shro8822 — 2008-05-08T17:09:40Z
try this foreach (ref string buf; f) there isn't a opApply with an index value, But IMHO there should be (converting to feature request)
Comment #2 by jlquinn — 2008-05-08T17:18:09Z
The code becomes: foreach (ref string buf; f) { which the compiler still dislikes: lexicon.d(92): function std.stream.Stream.opApply (int delegate(ref char[] line)) does not match parameter types (int delegate(ref invariant(char)[] buf)) lexicon.d(92): Error: cannot implicitly convert expression (__foreachbody15) of type int delegate(ref invariant(char)[] buf) to int delegate(ref ulong n, ref wchar[] line) Changing back to a bug :-) BTW, why is "ref" needed? If so, the docs aren't clear about that and need to be updated.
Comment #3 by shro8822 — 2008-05-08T17:34:24Z
If it's a bug, than it's a bug in DMD. If it's related to phobos than it's a feature request because it's operating correctly, just not the way we want it to. I use D1.0 so I can't test it but try this foreach (ref char[] buf; f) {
Comment #4 by jlquinn — 2008-05-08T20:28:35Z
(In reply to comment #3) > If it's a bug, than it's a bug in DMD. If it's related to phobos than it's a > feature request because it's operating correctly, just not the way we want it > to. Well, the docs say it should work, so there is at least the bug that the docs don't align with the implementation, even if we accept the implementation as correct. > I use D1.0 so I can't test it but try this > > foreach (ref char[] buf; f) { I expect that should work, but I'm trying to use string where I can. I tend to find string as a type more aesthetically pleasing to work with.
Comment #5 by witold.baryluk+d — 2010-11-24T07:44:01Z
I just hit this bug. What is worse, example on page http://digitalmars.com/d/2.0/phobos/std_stream.html#opApply do not work exactly because of this bug!
Comment #6 by andrej.mitrovich — 2014-04-23T18:31:29Z
D2 docs fixed, the D1 docs are not wrong since string is not immutable in D1.