This one weird trick allows you to capture loop variables.

Ali Çehreli acehreli at yahoo.com
Tue Nov 19 21:43:10 UTC 2024


On 11/19/24 8:50 AM, Richard (Rikki) Andrew Cattermole wrote:
 > On 20/11/2024 5:43 AM, Ali Çehreli wrote:

 >> I've always thought opApply is one of the most brilliant
 >> parts of D. It's easy to see the curly braces of the foreach loop as a
 >> lambda anyway.
 >
 > There are other ways to do it, than just giving it a delegate.
 >
 > I.e. hidden state struct and then range over it.
 >
 > Still on my todo list to look into, as it solves the attribute problem
 > of opApply also.

Please also keep in mind, as mentioned elsewhere in this thread, e.g. 
tree traversal is trivial with opApply but not with ranges. That's 
because the delegate is there to call regardless of how deeply recursed 
we are in traversal. With ranges, a range must maintain the state of 
traversing.

So, I guess both styles have their strong points.

Ali



More information about the Digitalmars-d mailing list