adding delegate to opApply

Paul Backus snarwin at gmail.com
Mon Sep 2 14:20:11 UTC 2019


On Monday, 2 September 2019 at 12:43:31 UTC, berni wrote:
> I need to pass the delegate add_new_way somehow to opApply. 
> Here I managed this, by adding this delegate to the 
> remove-delegate and safe it away for further use. This works, 
> because if remove is never called, add_new_way is not used 
> either. But it's a little bit awkward. Is there a better way, 
> to achieve this? (Note 1: In the real world, point%2==1 is a 
> more complex function call. Note 2: The order of elements in 
> Way[] way doesn't matter, if that makes things easier.)

The delegate that gets passed to opApply is constructed from the 
body of the foreach loop; you don't (normally) pass it explicitly.

If you have an existing delegate that you want to use with 
opApply, the easiest way is like this:

void delegate(Thing) myDelegate = ...;

foreach(thing; things) {
     myDelegate(thing);
}
// Equivalent to: things.opApply((Thing t) => myDelegate(t))


More information about the Digitalmars-d-learn mailing list