Unexpected foreach lowering

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 11 05:42:27 PDT 2016


On 8/11/16 12:28 AM, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Wednesday, August 10, 2016 21:00:01 Lodovico Giaretta via Digitalmars-d-
> learn wrote:
>> Wow. Thanks. I didn't know the compiler would try opSlice. I will
>> file it.
>
> It does that so that you can use foreach with containers without having to
> call something on the container. The idea is that the container will
> implement opSlice and make it return a range over the container, and foreach
> will then use that range to iterate over the container.

I get that. But it shouldn't try opSlice *first* if the item itself is a 
range (and it does do this). Many random-access ranges define opSlice, 
and most of the time range[] returns this. But in this case, it doesn't.

But it's a no-op for ranges, why waste time calling it?

-Steve



More information about the Digitalmars-d-learn mailing list