Invalid foreach aggregate

Chris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 17 04:41:43 PST 2015


On Tuesday, 17 November 2015 at 12:22:22 UTC, Marc Schütz wrote:
>
> Ok, that's a strange implementation of opIndex(). Usually, a 
> parameter-less opIndex() is supposed to return a slice into the 
> full range, but yours returns a size_t, which of course can't 
> be iterated over.
>
> The change that made this stop working is:
> https://github.com/D-Programming-Language/dmd/pull/4948
>
> This contains, among others a fix for issue 14625 "opIndex() 
> doesn't work on foreach container iteration":
> https://issues.dlang.org/show_bug.cgi?id=14625
>
> This allows to iterate directly over containers, without 
> needing to slice them first. I guess it's a bit too eager, 
> because if the object is already iterable without slicing (as 
> in your example), it could just do that. On the other hand, 
> it's a corner case, and it might actually be preferable to 
> slice always, if the iterable supports it...
>
> In any case, I'd suggest you fix your opIndex(), except if 
> there's a really good reason it is as it is.

I see. Thanks for the explanation. What would be the easiest fix 
for this example?


More information about the Digitalmars-d-learn mailing list