digitalmars.D.learn - stdio.lines doesn't have popFront, but works with foreach
- Andrew Klaassen (29/29) Dec 11 2014 The docs for stdio.lines say that it's a struct. stdio.lines
- bearophile (5/7) Dec 11 2014 If you want a range use "myfile".File.byLine or
- Andrew Klaassen (5/12) Dec 11 2014 I know that there are other ways that'll work. I was just
- Steven Schveighoffer (11/24) Dec 11 2014 Look down a little bit further on that document, you will see the
- Kapps (5/34) Dec 11 2014 Ranges are one way of allowing foreach. The other is through the
- Andrew Klaassen (4/7) Dec 11 2014 Ah, so I just needed to keep reading down a few lines in the
The docs for stdio.lines say that it's a struct. stdio.lines works with foreach. The docs for foreach say: "Iteration over struct and class objects can be done with ranges. For foreach, this means the following properties and methods must be defined: .empty ... .front ... .popFront()" But when I try to access any of those properties or methods for stdio.lines, I get compilation errors: 1 import io = std.stdio; 2 3 void main(string[] args) { 4 5 auto infile = io.File(args[1], "r"); 6 7 auto filelines = io.lines(infile); 8 9 io.writeln(filelines.front); 10 io.writeln(filelines.empty); 11 io.writeln(filelines.popFront()); 12 } test_filechunking.d(9): Error: no property 'front' for type 'lines' test_filechunking.d(10): Error: no property 'empty' for type 'lines' test_filechunking.d(11): Error: no property 'popFront' for type 'lines' Why is this? I'm using ldc2 for compilation, FWIW. Andrew
Dec 11 2014
Andrew Klaassen:The docs for stdio.lines say that it's a struct. stdio.lines works with foreach.If you want a range use "myfile".File.byLine or "myfile".File.byLineCopy. Bye, bearophile
Dec 11 2014
On Thursday, 11 December 2014 at 20:17:50 UTC, bearophile wrote:Andrew Klaassen:I know that there are other ways that'll work. I was just curious about what appears to be a surprise in the way that foreach interacts with stdio.lines. AndrewThe docs for stdio.lines say that it's a struct. stdio.lines works with foreach.If you want a range use "myfile".File.byLine or "myfile".File.byLineCopy. Bye, bearophile
Dec 11 2014
On 12/11/14 3:21 PM, Andrew Klaassen wrote:On Thursday, 11 December 2014 at 20:17:50 UTC, bearophile wrote:Look down a little bit further on that document, you will see the section titled "Foreach over Structs and Classes with opApply" Hm.. I note the incorrect statement "If the range properties don't exist, then the foreach is defined by the opApply member function". This is not exactly true, the foreach statement defaults to opApply first, and will use range functions only if that doesn't exist. FYI, the reason stdio.lines works that way is because in general, ranges can only give you one value, but stdio.lines wants to give you the line number too. -SteveAndrew Klaassen:I know that there are other ways that'll work. I was just curious about what appears to be a surprise in the way that foreach interacts with stdio.lines.The docs for stdio.lines say that it's a struct. stdio.lines works with foreach.If you want a range use "myfile".File.byLine or "myfile".File.byLineCopy. Bye, bearophile
Dec 11 2014
On Thursday, 11 December 2014 at 20:11:21 UTC, Andrew Klaassen wrote:The docs for stdio.lines say that it's a struct. stdio.lines works with foreach. The docs for foreach say: "Iteration over struct and class objects can be done with ranges. For foreach, this means the following properties and methods must be defined: .empty ... .front ... .popFront()" But when I try to access any of those properties or methods for stdio.lines, I get compilation errors: 1 import io = std.stdio; 2 3 void main(string[] args) { 4 5 auto infile = io.File(args[1], "r"); 6 7 auto filelines = io.lines(infile); 8 9 io.writeln(filelines.front); 10 io.writeln(filelines.empty); 11 io.writeln(filelines.popFront()); 12 } test_filechunking.d(9): Error: no property 'front' for type 'lines' test_filechunking.d(10): Error: no property 'empty' for type 'lines' test_filechunking.d(11): Error: no property 'popFront' for type 'lines' Why is this? I'm using ldc2 for compilation, FWIW. AndrewRanges are one way of allowing foreach. The other is through the use of opApply, which is what std.stdio.lines does. http://dlang.org/statement.html#ForeachStatement
Dec 11 2014
On Thursday, 11 December 2014 at 20:37:09 UTC, Kapps wrote:Ranges are one way of allowing foreach. The other is through the use of opApply, which is what std.stdio.lines does. http://dlang.org/statement.html#ForeachStatementAh, so I just needed to keep reading down a few lines in the foreach docs. Thanks! Andrew
Dec 11 2014