I realise that the Java 8 lambda implementation is subject to change, but in lambda build b39, I've found that braces can only be omitted when the lambda expression returns a non-void type. For example, this compiles:
public class Collections8 {
public static void main(String[] args) {
Iterable<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.filter(e -> e.length() > 4).forEach(e -> { System.out.println(e); });
}
}
But removing the braces like this:
names.filter(e -> e.length() > 4).forEach(e -> System.out.println(e));
gives the error
Collections8.java:6: error: method forEach in interface Iterable<T> cannot be applied to given types;
names.filter(e -> e.length() > 4).forEach(e -> System.out.println(e));
^
required: Block<? super String>
found: lambda
reason: incompatible return type void in lambda expression
where T is a type-variable:
T extends Object declared in interface Iterable
Can anyone explain what's going on here?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…