Yield considered harmful

Nathan Bryant nathan.bryant at linkshare.com
Fri Jul 9 14:26:41 PDT 2010


> 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 } )

Not necessary, when one can write it like this:

Sets.filter(set, { o -> o instanceof String && ((String)o).contains(
"http") } )

Or in the more common case where your Set is a Set<String>, you can
write:

Sets.filter(set, { o -> o.contains("http") } )

That said, yield is bad. -1 on yield. (If an additional -1 vote is even
necessary now that you've pointed out Thread.yield)


More information about the lambda-dev mailing list