Invalid foreach aggregate

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 17 03:26:15 PST 2015


On Monday, 16 November 2015 at 18:18:51 UTC, Chris wrote:
> On Monday, 16 November 2015 at 17:57:53 UTC, opla wrote:
>> On Monday, 16 November 2015 at 16:55:29 UTC, Chris wrote:
>>> On Monday, 16 November 2015 at 16:49:19 UTC, Marc Schütz 
>>> wrote:
>>>> On Monday, 16 November 2015 at 16:44:27 UTC, Chris wrote:
>>>>> Updating my code from 2.067.1 to 2.069.1 (I skipped 2.068, 
>>>>> because I was too busy).
>>>>>
>>>>> I get this error:
>>>>>
>>>>> invalid foreach aggregate, define opApply(), range 
>>>>> primitives, or use .tupleof
>>>>>
>>>>> for code like
>>>>>
>>>>> foreach (ref it; myArray.doSomething) {}
>>>>>
>>>>> Probably not the best idea anyway. What's the best fix for 
>>>>> this? Thanks.
>>>>
>>>> Well, what does `doSomething` return?
>>>
>>> It returns a range that modifies individual items in myArray, 
>>> i.e. it assigns values to fields in each item of the array.
>>
>> have you...
>>
>> tried without ref
>> tried by adding a pair of parens after doSomething ?
>> tried std.algorithm.each or map on doSomething ?
>> checked the primitives ?
>> checked that isInputRange!(ReturnType!doSomething) == true?
>
> I think ref is necessary, else the items are not changed. I 
> will try the other options tomorrow (Tuesday). Thanks.
>
> I wonder was the change overdue (and I got away with it till 
> 2.068.1) or is it a new policy due to changes in D?

That really depends on the details, that's why I asked. It could 
be a regression, or it could be that the compiler now does 
stricter checking than before, and your implementation wasn't 
completely correct, or it could be a bug in Phobos if you use 
that to create the range.

If you can post a minimal example that works in 2.067.1, but 
doesn't with the current version, I can try to find the change 
that broke it.


More information about the Digitalmars-d-learn mailing list