opApply not called for foeach(container)

Steven Schveighoffer schveiguy at yahoo.com
Fri Jul 13 05:54:42 PDT 2012


On Fri, 13 Jul 2012 02:20:45 -0400, monarch_dodra  
<monarch_dodra at gmail.com> wrote:

> On Thursday, 12 July 2012 at 22:02:19 UTC, Steven Schveighoffer wrote:
>> On Thu, 12 Jul 2012 17:35:23 -0400, monarch_dodra  
>> <monarch_dodra at gmail.com> wrote:
>>
>>> On Thursday, 12 July 2012 at 21:18:21 UTC, Steven Schveighoffer wrote:
>>>>
>>>> If opSlice is to be used, this is how it should go (in order of  
>>>> precedence):
>>>>
>>>> 1. if aggr has opApply or opApplyReverse, use it.
>>>>
>>>> 2. if aggr has opSlice, and the result of aggr.opSlice() has opApply  
>>>> or opApplyReverse, use it.
>>>>
>>>> 3. if aggr has opSlice, and the result of aggr.opSlice() has  
>>>> empty/front/popfront, use it as in your 2a above.
>>>>
>>>> 4. if aggr has empty/front/popFront, use it as in your 2b above.
>>>>
>>>> 5. static or dynamic array.
>>>>
>>>> I should also note that the existence of opApply should not preclude  
>>>> later possibilities if that opApply can't compile for the given  
>>>> foreach parameters.
>>>>
>>>> -Steve
>>>
>>> 4.1:Make copy first.
>>
>> Kenji's 2b does do that:
>>
>>
>>   for (auto __r = aggr;  // If aggr is copyable, saves the original  
>> range.
>>       !__r.empty;
>>       __r.popFront()) { auto e = __r.front; ...body... }
>>
>>
>> -Steve
>
> Oh, right "use it as in _your_ 2b above". Didn't get what you meant at  
> first.

hehe, sorry.  Should have said Kenji's...

>
> About the if "opApply can't compile": Do you mean:
> a) If there is no matching opApply function?
> b) Or if there is an actual error in the body of opApply?
> I think you meant a) ?
>
> I think your 1. and 2. should instead read:
> 1. if aggr has a matching opApply or opApplyReverse, use it.
> 2. if aggr has opSlice, and the result of aggr.opSlice() has a matching  
> opApply or opApplyReverse, use it.

Yes, I meant matching.  Meaning if I define front to return int, and  
opApply takes a delegate for a float, then foreach(int x; aggr) should use  
the range primitives, foreach(float x; aggr) should use opApply.

-Steve


More information about the Digitalmars-d mailing list