Tricky semantics of ranges & potentially numerous Phobos bugs
    monarch_dodra 
    monarchdodra at gmail.com
       
    Thu Oct 18 00:40:08 PDT 2012
    
    
  
On Thursday, 18 October 2012 at 07:09:04 UTC, Don Clugston wrote:
> On 17/10/12 23:41, H. S. Teoh wrote:
>
> Is it actually orthogonal? Is it possible for a forward range 
> to be transient?
>
> Or is it an intermediate concept?
> TransientInputRange -> NonTransientInputRange -> ForwardRange
Save just means the range can save its position. If it is 
returning via a buffer, Forward of not, it is going to be 
transient.
Take this forward range, that returns the strings "A", "B" and 
"C" ad infinitum:
//----
enum _ABC = "ABC";
struct ABC
{
     char[1] buf = _ABC[0];
     size_t i;
     enum empty = false;
     @property char[] front(){return buf;}
     void popFront()
     {
         ++i;
         buf[0] = _ABC[i%3];
     }
     @property ABC save()
     {
         return this;
     }
}
//----
This is a perfectly valid range, which you can save, but the 
returned string is transient:
//----
void main()
{
   ABC abc;
   writeln("Printing 10 elements: ");
   abc.take(10).writeln('\n');
   writeln("Duplicating range");
   auto abc2 = abc.save;
   abc.popFront;
   foreach(v; zip(abc, abc2).take(5))
     write("[", v[0], ", ", v[1], "]");
   writeln('\n');
   writeln("Prnting two consecutive elements:");
   auto first = abc.front;
   abc.popFront();
   auto second = abc.front;
   writeln("[", first, ", ", second, "]");
}
//----
Produces:
//----
Printing 10 elements:
["A", "B", "C", "A", "B", "C", "A", "B", "C", "A"]
Duplicating range
[B, A][C, B][A, C][B, A][C, B]
Prnting two consecutive elements:
[C, C]
//----
As you can see, you can perfectly iterate.
You can perfectly save the range. The saved range can be used to 
backtrack.
But if you attempt to store two consecutive fronts, things don't 
go well.
The same holds true for a Random Access range BTW.
Iteration and transient-ness of returned value are orthogonal 
concepts
    
    
More information about the Digitalmars-d
mailing list