Java 8 lambda Void argument -
let's have following functional interface in java 8:
interface action<t, u> { u execute(t t); } and cases need action without arguments or return type. write this:
action<void, void> = () -> { system.out.println("do nothing!"); }; however, gives me compile error, need write as
action<void, void> = (void v) -> { system.out.println("do nothing!"); return null;}; which ugly. there way rid of void type?
the syntax you're after possible little helper function converts runnable action<void, void> (you can place in action example):
public static action<void, void> action(runnable runnable) { return (v) -> { runnable.run(); return null; }; } // somewhere else in code action<void, void> action = action(() -> system.out.println("foo"));
Comments
Post a Comment