Search code examples
javaeclipselambdafunctional-interface

Lambda type missmatch in Eclipse


I am trying to use the lambda operator, I have managed to do similar things with other methods, but this one is giving out an error.

Methods in question:

public Icon setOnClick(IconClick e) {
    this.click = e;
    return this;
}

and:

public interface IconClick {
    boolean onClick(Player p, Icon i, InventoryClickEvent e);
}

And here, I tried to use lambda:

.setOnClick((Player p, Icon i, InventoryClickEvent e) -> {
        //Code in here
    }));

But Eclipse keeps giving this error:

The method setOnClick(IconClick) in the type Icon is not applicable for the arguments ((Player p, Icon i, InventoryClickEvent e) -> {})

It evens suggest to create a method with arguments IconClick in the Icon class, but that`s method I'm trying to use.

(Using Java 8, compiler compliance level: 1.8 and Eclipse version: 2018-12 (4.10.0))


Solution

  • You might just be missing the return type there :

    IconClick onClick = (Player p, Icon i, InventoryClickEvent e) -> {
         // some logic in here
         return false; // must return 'false' or 'true' 
    };
    
    Icon icon = new Icon().setOnClick((p, i, e) -> false); // or 'onClick'