The OpenD Programming Language

Generator

A Generator is a Fiber that periodically returns values of type T to the caller via yield. This is represented as an InputRange.

Constructors

this
this(void function() fn)

Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.

this
this(void function() fn, size_t sz)

Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.

this
this(void function() fn, size_t sz, size_t guardPageSize)

Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.

this
this(void delegate() dg)

Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.

this
this(void delegate() dg, size_t sz)

Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.

this
this(void delegate() dg, size_t sz, size_t guardPageSize)

Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.

Members

Functions

empty
bool empty()

Returns true if the generator is empty.

front
T front()

Returns the most recently generated value by shallow copy.

moveFront
T moveFront()

Returns the most recently generated value without executing a copy contructor. Will not compile for element types defining a postblit, because Generator does not return by reference.

popFront
void popFront()

Obtains the next value from the underlying function.

Inherited Members

From Fiber

~this
~this()

Cleans up any remaining resources used by this object.

call
Throwable call(Rethrow rethrow)
Throwable call()

Transfers execution to this fiber object. The calling context will be suspended until the fiber calls Fiber.yield() or until it terminates via an unhandled exception.

Rethrow
enum Rethrow

Flag to control rethrow behavior of $(LREF call)

reset
void reset()
void reset(void function() fn)
void reset(void delegate() dg)

Resets this fiber so that it may be re-used, optionally with a new function/delegate. This routine should only be called for fibers that have terminated, as doing otherwise could result in scope-dependent functionality that is not executed. Stack-based classes, for example, may not be cleaned up properly if a fiber is reset before it has terminated.

State
enum State

//////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// A fiber may occupy one of three states: HOLD, EXEC, and TERM.

state
State state [@property getter]

Gets the current state of this fiber.

yield
void yield()

Forces a context switch to occur away from the calling fiber.

yieldAndThrow
void yieldAndThrow(Throwable t)

Forces a context switch to occur away from the calling fiber and then throws obj in the calling fiber.

getThis
Fiber getThis()

Provides a reference to the calling fiber or null if no fiber is currently active.

Examples

auto tid = spawn({
    int i;
    while (i < 9)
        i = receiveOnly!int;

    ownerTid.send(i * 2);
});

auto r = new Generator!int({
    foreach (i; 1 .. 10)
        yield(i);
});

foreach (e; r)
    tid.send(e);

assert(receiveOnly!int == 18);

Meta