User defined type and foreach

Tony tonytdominguez at aol.com
Thu Nov 16 08:43:17 UTC 2017


On Thursday, 16 November 2017 at 08:26:25 UTC, Andrea Fontana 
wrote:
> On Thursday, 16 November 2017 at 08:03:48 UTC, Tony wrote:
>> I made a stack data type and created an opIndex() so it could 
>> be turned into a dynamic array, and created empty() 
>> (unfortunate name), front() and popFront() methods, which I 
>> read allow it to be used with foreach.
>>
>> However, when I use the class with foreach, the opindex gets 
>> called to create a dynamic array, rather than use the 
>> empty(),front(),popFront() routines. I would prefer it use the 
>> three methods, rather than create a dynamic array.
>
> You can try to implement opApply().
> Check:
> http://ddili.org/ders/d.en/foreach_opapply.html

Thanks. Interesting that that page does not mention the behavior 
I am seeing, which is that foreach over a user-defined datatype 
can be implemented with only a 'T[] opIndex()' method.


More information about the Digitalmars-d-learn mailing list