A beginner's question of foreach

torhu no at spam.invalid
Sun Dec 30 17:06:38 PST 2007


Alex wrote:
> The code snippet from the document page of std.stream can't pass compile:
> InputStream s = new BufferedFile(file);
> 
> foreach(ulong n, string line; s)
> {
> 	writeln(line);
> }
> 
> The following are error message:
> D:\Tools\d\dmd>dmd cat.d
> cat.d(11): function std.stream.InputStream.opApply (int delegate(ref char[] line)) does not match parameter types (int delegate(ref ulong __applyArg0, ref invariant(char)[] __applyArg1))cat.d(11): Error: cannot implicitly convert expression (__foreachbody9) of typeint delegate(ref ulong __applyArg0, ref invariant(char)[] __applyArg1) to int delegate(ref ulong n, ref wchar[] line)
> 
> 
> Any idea? The document needs update?

There's no opApply overload taking an invariant(char)[].  You can use 
dmd 1.x compilers until things have settled down in 2.x.  Or you just 
use a char[] instead of the string alias.  If you need the string to be 
invariant you can cast it to string before using it.

According to the docs for InputStream, opApply is allowed to reuse the 
char buffer, so depending on your needs, you might have to use .idup or 
.dup.



More information about the Digitalmars-d mailing list