Should range foreach be iterating over an implicit copy?

Steven Schveighoffer schveiguy at yahoo.com
Thu May 17 07:18:55 PDT 2012


On Thu, 17 May 2012 01:48:06 -0400, Nick Sabalausky  
<SeeWebsiteToContactMe at semitwist.com> wrote:

> I'm a little discouraged that my concern about "input ranges can't save
> their state, and yet that's exactly what happens implicitly" hasn't been
> addressed. I was hoping to at least get a "That's not really a problem  
> and
> here's why..."

input ranges can save their state if they are also forward ranges.

>
> However, that said...
>
> "Andrei Alexandrescu" wrote...
>> It is deliberate and the intent is that millions of programmers used to
>> foreach from other languages don't scream "where is my range???"
>
> "Era Scarecrow" wrote...
>> Range can refer to many things, and I'm assuming array is one of them.
>> So... If the array is consumed when using foreach, that seems dumb  
>> right?
>> (An array in the end is just a small struct afterall...)
>
> I admit, those are fair points.
>
> With those in mind, I've given it some more thought, and here are my
> current...umm...thoughts:

[snip]

This is not a problem with ranges, this is an issue with value types  
versus reference types.  I believe someone has created a byRef struct that  
wraps a range and iterates it byRef (maybe dsimcha?)

Almost all ranges except true input-only ranges (i.e. input ranges that  
are only input ranges) should be value types.

If we *didn't* do this, you would need heap data for each range.

-Steve


More information about the Digitalmars-d mailing list