How come a count of a range becomes 0 before a foreach?

Steven Schveighoffer schveiguy at gmail.com
Sun Apr 9 03:39:52 UTC 2023


On 4/8/23 9:38 PM, ikelaiah wrote:
> // Get files in specified inputPath variable with a specific extension
>      auto rmdFiles = file.dirEntries(inputPath, file.SpanMode.shallow)
>          .filter!(f => f.isFile)
>          .filter!(f => f.name.endsWith(fileEndsWith));
> 
>      // LINE 72 -- WARNING -- If we count the range here, later it will 
> become 0 in line 82
>      writeln(programName ~ ": number of files found " ~ 
> to!string(rmdFiles.walkLength));

dirEntries returns an *input range*, not a *forward range*. This means 
that once it's iterated, it's done.

If you want to iterate it twice, you'll have to construct it twice.

-Steve


More information about the Digitalmars-d-learn mailing list