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

ikelaiah iwan.kelaiah at gmail.com
Sun Apr 9 04:38:11 UTC 2023


On Sunday, 9 April 2023 at 03:39:52 UTC, Steven Schveighoffer 
wrote:
> 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

Steve,

You're absolutely right. I did not read the manual correctly.

It is clearly written 
[here](https://dlang.org/library/std/file/dir_entries.html) that 
`dirEntry` is an `input range`.

I will modify the code to construct it twice.
Many thanks!

-ikelaiah


More information about the Digitalmars-d-learn mailing list