D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2081 - Foreach over Stream appears broken
Summary: Foreach over Stream appears broken
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-05-08 16:58 UTC by Jerry Quinn
Modified: 2014-04-23 18:31 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jerry Quinn 2008-05-08 16:58:26 UTC
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 BCS 2008-05-08 17:09:40 UTC
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 Jerry Quinn 2008-05-08 17:18:09 UTC
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 BCS 2008-05-08 17:34:24 UTC
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 Jerry Quinn 2008-05-08 20:28:35 UTC
(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 Witold Baryluk 2010-11-24 07:44:01 UTC
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 Andrej Mitrovic 2014-04-23 18:31:29 UTC
D2 docs fixed, the D1 docs are not wrong since string is not immutable in D1.