foreach / mutating iterator - How to do this?

Robert M. Münch robert.muench at saphirion.com
Tue Jun 26 05:55:32 UTC 2018


On 2018-06-25 15:29:23 +0000, Robert M. Münch said:

> I have two foreach loops where the inner should change the iterator 
> (append new entries) of the outer.
> 
> foreach(a, candidates) {
> 	foreach(b, a) {
> 		if(...) candidates ~= additionalCandidate;
> 	}
> }
> 
> The foreach docs state that the collection must not change during iteration.
> 
> So, how to best handle such a situation then? Using a plain for loop?

Answering myself: If you implement an opApply using for() or while() 
etc. with a mutating aggregate, foreach can be used indirectly with 
mutating aggregates. Works without any problems.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list