foreach() behavior on ranges

Steven Schveighoffer schveiguy at gmail.com
Wed Aug 25 11:02:23 UTC 2021


On 8/25/21 4:31 AM, frame wrote:
> On Tuesday, 24 August 2021 at 21:15:02 UTC, Steven Schveighoffer wrote:
>> I'm surprised you bring PHP as an example, as it appears their foreach 
>> interface works EXACTLY as D does:
> 
> Yeah, but the point is, there is a rewind() method. That is called every 
> time on foreach().

It seems what you are after is forward ranges. Those are able to 
"rewind" when you are done with them. It's just not done through a 
rewind method, but via saving the range before iteration:

```d
foreach(val; forwardRange.save)
{
    ...
    break;
}

// forwardRange hasn't been iterated here
```

-Steve


More information about the Digitalmars-d-learn mailing list