Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert void to java.lang.Void

Tags:

I'm trying to do the following

interface Updater {     void update(String value); }  void update(Collection<String> values, Updater updater) {     update(values, updater::update, 0); }  void update(Collection<String> values, Function<String, Void> fn, int ignored) {     // some code } 

but I get this compiler error:

"Cannot convert void to java.lang.Void" 

That means updater::update cannot be used as Function<String, Void>.

Of course I can't write Function <String, void> and I don't want to change return type of update() to Void.

How do I resolve this problem?

like image 599
Kastaneda Avatar asked Feb 27 '15 10:02

Kastaneda


1 Answers

A Function returns a value, even if it is declared as being of type Void (you will have to return null then. In contrast, a void method really returns nothing, not even null. So you have to insert the return statement:

void update(Collection<String> values, Updater updater) {     update(values, s -> { updater.update(); return null; }, 0); } 

An alternative would be to change the Function<String,Void> to Consumer<String>, then you can use the method reference:

void update(Collection<String> values, Updater updater) {     update(values, updater::update, 0); } void update(Collection<String> values, Consumer<String> fn, int ignored) {     // some code } 
like image 132
Holger Avatar answered Nov 03 '22 05:11

Holger