opApply & opApplyReverse Member Functions
Explore how to implement and use opApply and opApplyReverse member functions in D to enable custom iteration in foreach loops. Understand the convention behind opApply calls, how to write them for structs and classes, and how overloads allow iterating an object in multiple ways. This lesson equips you to design flexible and efficient loops for your D types.
We'll cover the following...
foreach, opApply, and opApplyReverse
Everything that is said about opApply in this section is valid for opApplyReverse as well. opApplyReverse is for defining the behaviors of objects in the foreach_reverse loops.
The member functions above allow us to use objects as ranges. That method is more suitable when there is only one sensible way of iterating over a range. For example, it would be easy to provide access to individual students of a Students type.
On the other ...