yield4j adds generator methods to Java through annotation processing magic.
Currently, yield4j must be both a compile-time dependency and a runtime dependency. That may change in the future.
Here's an example of a generator method that generates numbers from the Fibonacci sequence:
public class Fibonacci {
@Generator
public Iterable<Integer> fib() {
yield_return(0);
yield_return(1);
int i1 = 0, i2 = 1;
while (true) {
int next = i1 + i2;
yield_return(next);
i1 = i2;
i2 = next;
}
}
}
Use yield_return(X)
to generate value X
. The yield_return
method is a static method of org.yield4j.YieldSupport
.
To stop/quit the generator, use yield_break()
.
- A generator method cannot contain a normal
return
statement. - No yielding within a
synchronized
statement (generator methods can be synchronized, though). yield_break
cannot be used in afinally
statement.yield_return
cannot be used in acatch
orfinally
statement.
In general, the yield_*
statements behave much as their C# counterparts.
Code in a finally
statement may not be executed if a generator is stopped before execution reaches that point.
yield4j
works with Java 6, 7 and 8. It was a while ago I tested with Java 8, so it may be shaky there.
To compile and test with all currently installed Java versions on Windows, run buildall.bat
. Maven is required.
Just include the yield4j
JAR in the class path. Make sure to not turn off annotation processing.
See the TODO file.
A big TODO is to add some sort of editor/IDE support...
MIT license. See LICENSE.md.