archives

Extending Java with Yield

The Yielder library by infomancers (a.k.a. Aviad Ben Dov of the Chaotic Java weblog) is a library providing Java with Ruby/Python/C#-like yield-ing for iterators.

It works by using Java 1.5's facility for hooking user-defined class inspectors/transformers into the classloader, and rewriting the bytecode of a yieldNextCore() method containing calls to yieldReturn(foo) so that its local variables are promoted to members of an anonymous inner class, which also maintains the state of the iterator/generator.

Examples, rationale and implementation are discussed in detail at Chaotic Java. There is also a collections library built on top of Yielder, with tree traversal iterators and map/filter-like functionality.

From a PL perspective, this ability to hook into the classloader and do bytecode manipulation on plain java classes provides an interesting mechanism for prototyping new language features in libraries. It could equally well support an aspect framework (via annotations), and perhaps even something like Terracotta's distributed heap.

Jedi

Jedi aims to bring a more functional style of programming to Java. It does so by providing a library of routines for filtering, iteration, conversion from one collection type to another, first order logic functions, etc. It makes extensive use of higher order functions and closures. Unlike most libraries with similar aims, Jedi tries to take the hard work out of producing the required closures by providing JDK 1.5 annotations which generates the necessary filters, commands and functors.

Jedi has been used on a number of commercial applications and has been found to significantly simplify the code.