I ran into an issue where the allowed syntax of a Lambda has changed between versions 1.8.0_05 and 1.8.0_20 (beta) of the java compiler.
Example:
package scratch;
import javafx.scene.control.MenuItem;
public class Test
{
public void test()
{
MenuItem mi = new MenuItem();
//This compiles anywhere
mi.setOnAction(e -> System.out.println("hi"));
//as does this
mi.setOnAction(e -> {System.out.println("hi");});
//This doesn't on build 1.8.0_20-ea-b13 - but does on build 1.8.0_05-b13
mi.setOnAction(e -> (System.out.println("hi")));
}
}
What I would like to know - is the last example a valid Lambda expression? And they have just tightened the compiler validation? Or is there a bug in the latest 1.8 compiler?
The error printed by the latest compiler is:
/scratch/src/scratch/Test.java:18: error: method setOnAction in class MenuItem cannot be applied to given types;
mi.setOnAction(e -> (System.out.println("hi")));
^
required: EventHandler<ActionEvent>
found: (e)->(Syst[...]hi"))
reason: argument mismatch; bad return type in lambda expression
missing return value
1 error
Edit (since I can't seem to format comments in replies):
The implementation of the setOnAction method is:
public final void setOnAction(EventHandler<ActionEvent> value) {
onActionProperty().set( value);
}
And EventHandler:
@FunctionalInterface
public interface EventHandler<T extends Event> extends EventListener {
/**
* Invoked when a specific event of the type for which this handler is
* registered happens.
*
* @param event the event which occurred
*/
void handle(T event);
}