www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2081] New: Foreach over Stream appears broken

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2081

           Summary: Foreach over Stream appears broken
           Product: D
           Version: 2.012
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: jlquinn optonline.net


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)


-- 
May 08 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2081


shro8822 vandals.uidaho.edu changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement





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)


-- 
May 08 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2081


jlquinn optonline.net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|enhancement                 |normal





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.


-- 
May 08 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2081






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) {


-- 
May 08 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2081







 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. --
May 08 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2081


Witold Baryluk <baryluk smp.if.uj.edu.pl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |baryluk smp.if.uj.edu.pl



07:44:01 PST ---
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!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 24 2010