Lots of low hanging fruit in Phobos

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Thu Mar 6 19:37:53 PST 2014


On 3/6/2014 8:37 PM, H. S. Teoh wrote:
>
> Unfortunately, input ranges are somewhat tedious to write -- nice
> foreach loops have to be broken up into .empty, .front, .popFront, which
> is a lot of boilerplate code and not so nice in inner loops (though I
> suppose the idea is to improve compiler inlining to handle these cases).
> I wonder if a mid- to long-term goal for D would be to ease writing
> input ranges by moving some of the boilerplate into the language, or
> providing range builder facilities in Phobos. The most nagging part of
> writing input ranges in the current language is the inability to use
> foreach to generate the returned elements. (Well, you *can* do that to a
> buffer array and then return the array, but that kinda defeats the
> purpose of GC avoidance.) Some kind of built-in coroutine syntax with
> 'yield' would help things a LOT.
>

Yes, this has been #1 on my wishlist for ranges for some time. We really 
need a way to make input ranges (maybe even forward ranges if we're 
clever enough about it) via "coroutines" that, like C#'s coroutines, are 
automatically converted behind-the-scenes into stackless fibers (ie, 
into state machines).

A while back, I tried doing a library solution for this via regular 
fibers, but there turned out to be a lot of overhead due to the fiber's 
context-switching [1].

We need to take inspiration from two things: C's "protothreads" 
library[2], and (as I said) C#'s coroutines. Both of those are fantastic 
IMO. In both cases, the body of a coroutine is automagically transformed 
into a big switch statement. Yield statements then become roughly 
"return tuple([yielded value], [resume point]); case [resume 
point]:...". And then invoking the coroutine again automatically passes 
in the last [resume point] returned so the big switch() can jump 
straight to where it left off. The main difference in our case would be 
that we'd also auto-generate all the boilerplate rigging for this to be 
an input range.

[1] 
http://semitwist.com/articles/article/view/combine-coroutines-and-input-ranges-for-dead-simple-d-iteration

[2] C's "protothreads" library: http://dunkels.com/adam/pt/expansion.html



More information about the Digitalmars-d mailing list