Presenting Fibers as Ranges
Although we have achieved generating the Fibonacci series with a fiber, this implementation has the following shortcomings:
The solution does not provide a range interface, making it incompatible with existing range algorithms.
Presenting the elements by mutating a
refparameter is less desirable compared to a design where the elements are copied to the caller’s context.
Constructing and using the fiber explicitly through its member functions exposes lower level implementation details, compared to alternative designs.
std.concurrency.Generator class addresses all of these issues. Note how
fibonacciSeries() below is written as a simple function. The only difference is that, instead of returning a single element by return, it can make multiple elements available by
yield() (infinite elements in this example).
Also, note that, this time, it is the
std.concurrency.yield function, not the
Fiber.yield member function that we used above.