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)
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)
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.
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) {
(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.
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!
D2 docs fixed, the D1 docs are not wrong since string is not immutable in D1.