Lots of low hanging fruit in Phobos

Adam D. Ruppe destructionator at gmail.com
Fri Mar 7 18:15:18 PST 2014


On Saturday, 8 March 2014 at 01:10:38 UTC, H. S. Teoh wrote:
> Having a way to auto-generate input range boilerplate, though, 
> would be really, *really* nice.

Eh, I don't think it is a big deal and would be fairly limited 
compared to the current setup. If you use a fiber or state 
variable or something for the yield this yield that trick, how do 
you go backward? Random access?

I think the best the yield stuff can do is maybe forward range 
and maybe infinite (probably with an annotation of some sort, 
since otherwise, the infiniteness wouldn't be obvious at compile 
time).


So the best we're looking to automate is input or perhaps forward 
ranges. And how hard are these really to write?

yield query(string q) {
    auto result = c_query(toStringz(q));
    while(!HasRow(result))
       yield GetNextRow(result);
}

OK, that is kinda nice, but, is the status quo so bad? (BTW the 
reason I went with some kind of C database api here is everything 
else I could think of are actually pretty short when using 
std.algorithm functions to help define them.)

struct query {
     private Result result;
     this(string q) {
          result = c_query(toStringz(q));
          if(!empty) popFront();
     }

     Row front;
     @property bool empty() { return HasRow(result); }
     void popFront() in { assert(!empty); } body {
          front = GetNextRow(result);
     }
}


It is certainly a bit longer, but it isn't that bad, and is 
easily extended to other range capabilities.


Translating recursive iteration to a range does take a bit more, 
you need to track your local variables and put them in a stack of 
your own, but even that isn't too hard (though a bit wordier).


I guess the whole yield thing can be kinda nice, I'm just not 
sure it is that big of a win given its other limitations compared 
to full ranges.


More information about the Digitalmars-d mailing list