foreach() behavior on ranges

frame frame86 at live.com
Thu Aug 26 15:18:41 UTC 2021


On Wednesday, 25 August 2021 at 11:02:23 UTC, Steven 
Schveighoffer wrote:
> 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

This could be any custom method for my ranges or forward range 
returned by some function.

But that doesn't help if some thirdparty library function would 
break and return just an input range. Then it seems that it must 
be very properly implemented like postblit technics mentioned 
before. Some author may never care about.

That it works in 99% of all cases should not be an excuse for a 
design flaw.
The documentation really need to mention this.





More information about the Digitalmars-d-learn mailing list