Yield considered harmful

Alex Blewitt alex.blewitt at gmail.com
Fri Jul 9 14:18:19 PDT 2010

As has been noted elsewhere, the inconsistency between 'this' as a reference to the SAM instance and 'yield' as a way of return-from-lambda is not a good development. Given that 'this' is already defiend, why not use 'return' as well? Almost all Java developers will understand this immediately; and it doesn't prevent the kind of macro use that has been described about elsewhere in the future. 

Furthermore, 'yield' already has an existant meaning in Java:


"Causes the currently executing thread object to temporarily pause and allow other threads to execute."

Having a keyword mean something completely different from a well defined term in multi-threading (and one that's been there since Java 1.0 days) seems folly at best. 

Furthermore, note that many people will want to take advantage of the smaller syntax in existing filter operations. For example, Google's guava provides Predicate


Examples like:

Sets.filter(set, new Predicate() {
  boolean apply(Object o) {
    if (o instanceof String && ((String)o).contains("http") )
     return true;
    return false;
} );

could easily take advantage of the new syntax:

Sets.filter(set, { o -> if(o instanceof String && ((String)o).contains( "http")) yield true; yield false } )

however, note that as well as removing the boilerplate, we also have to re-write the 'return' for 'yield' for no practically obvious reason.

Even if non-local returns are added at a later stage, there's no reason that that could not use a new syntax and avoid the complications of misusing return now.


More information about the lambda-dev mailing list