It took a lot of effort to develop a lambda expression to handle int/Integer , etc. indistinguishable, so int->Long will be compatible with Integer->long , etc.
It is possible (and desirable) to relate to void/Void similar way, see comments by Goetz and Forax . However, they did not have time to implement the idea for java8 :(
You can enter an adapter type that is ()->void and ()->void ; this may simplify your use case a bit, see http://bayou.io/release/0.9/javadoc/bayou/util/function/Callable_Void.html
If you have a method that accepts ()->void , it will not work well with ()->void lambdas. One way is to overload the method to accept ()->void . For example, ExecutorService
submit(Callable<T> task) submit(Runnable task) ... submit( System::gc );
However, overloading with the types of functional parameters is complicated ... The above example works because both accept a zero-arg function. If the function has nonzero arguments
foo( Function<String,Void> f )
it confuses the compiler (and the programmer)
foo( str->System.out.println(str) ); // which foo? foo( System.out::println ); // which foo?
Given the implicit lambda str->expr , the compiler needs a target type to understand it. The type of target here is set by the type of the method parameter. If the method is overloaded, we need to first allow the method to be overloaded ... which usually depends on the type of argument (lambda) ... Therefore, you can understand why this is difficult.
(Zero-arg lambda is never implicit. All argument types are known because there are no arguments.)
There are provisions in the lambda specification to resolve the following cases:
foo( str->{ System.out.println(str); } ); foo( str->{ System.out.println(str); return null; } );
You can argue that in the previous example
foo( str->System.out.println(str) );
since println(str) returns void , the version of void clearly not suitable, so the compiler should be able to resolve it. However, remember that in order to know the value of println(str) , you must first enable the str type, that is, overloading the foo method must first be resolved ...
Although in this case str explicitly String . Unfortunately, the lambda designer decided not to allow this, arguing that it was too complicated. This is a serious flaw, which is why we cannot overload methods like Comparator
comparing( T->U )