More tricky range semantics

Tobias Pankrath via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 16 09:09:46 PST 2015


On Friday, 16 January 2015 at 16:12:23 UTC, Joseph Rushton 
Wakeling wrote:
> void foo (FRange) (FRange range)
>     if (isForwardRange!FRange)
> {
>     foreach (i; 0 .. 10)
>     {
>         // silly example :-P
>         auto r = range.save;
>         r.take(10).writeln;
>     }
> }
>
> This is a problematic design if FRange is a reference type, 
> because (by design) if the values in it are used, they should 
> be consumed.  So e.g. if you were passing a reference-type RNG 
> to a function that does this, you'd like to guarantee that (i) 
> the function is able to use the ability to repeat the sequence, 
> but (ii) consumes from the original exactly once.
>
> If you don't get that, you will wind up with unintended 
> correlations in your use of random numbers.

While the first example is indeed problematic, this one actually 
is not. If this does not print the same 10 numbers every time, 
your save method is wrong.
Regardless of being a reference type or not it has to clone the 
RNG state or it doesn't do its job.



More information about the Digitalmars-d mailing list