Several times we have already wished to see yield feature in java and all the time came to the same implementation: infomancers-collections. And every time with dissatisfaction turned away, and continued with regular iterators.
yield
Why? Well, in spite of the fact it's the best implementation of the feature we have seen, it's still too heavy, as it's playing with java byte code at run-time.
We never grasped the idea why it's done this way, while there is post-compile time annotation processing in java.
If we would implemented the yeild feature in java we would created a @Yield annotation and would demanded to implement some well defined code pattern like this:
@Yield
@Yield Iteratable<String> iterator() { // This is part of pattern. ArrayList<String> list = new ArrayList<String>(); for(int i = 0; i < 10; ++i) { // list.add() plays the role of yield return. list.add(String.valueOf(i)); } // This is part of pattern. return list; }
or
@Yield Iterator<String> iterator() { // This is part of pattern. ArrayList<String> list = new ArrayList<String>(); for(int i = 0; i < 10; ++i) { // list.add() plays the role of yield return. list.add(String.valueOf(i)); } // This is part of pattern. return list.iterator(); }
Note that the code will work correctly even, if by mischance, post-compile-time processing will not take place.
At post comile time we would do all required refactoring to turn these implementations into a state machines thus runtime would not contain any third party components.
It's iteresting to recall that we have also implemented similar refactoring in pure xslt.
See What you can do with jxom.
Update: implementation can be found at Yield.zip
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u