0

How do I get IntelliJ to auto-generate a lambda expression as the argument being passed?

enter image description here

What I want:

enter image description here

I have seen the Question How to autocomplete lambdas in IntelliJ IDEA?, but that does not seem to produce my desired result.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Why do you want to have the typecast in your code? It's unnecessary. – yole Mar 24 '18 at 21:39
  • @yole (a) I think your query is irrelevant to the Question, but perhaps I misunderstand. (b) That was created by IntelliJ after I typed `new Clic` (or some such). IntelliJ 2018.3 offers helpers to convert that piece to various forms. I don't care which form I get, I just don't want to have to type in the listener boilerplate at all. In my first screenshot you can see that IntelliJ knows a `ClickListener` should be passed. I want *some* `ClickListener` to be provided for me. I'll be happy with *any* syntax form of a `ClickListener` implementation that IntelliJ can auto-create. – Basil Bourque Mar 24 '18 at 22:14
  • Doesn't Ctrl+Shift+Space inside parentheses work for you? For me it suggests `event -> {}` which is pretty close to your desired result. – Tagir Valeev Mar 25 '18 at 01:51

1 Answers1

1

As Tagir Valeev already mentioned in his comment, IntelliJ will auto-generate lambda code, but only if your ClickListener is a (functional) interface.

E.g.:

@FunctionalInterface
interface ClickListener {
  void listen();
}

class Clicker {
   void addClickListener(ClickListener listener){
      // ...
   }
}

Then, Intellij suggests upon hitting Ctrl+Shift+Space

enter image description here

Note: As to the @FunctionalInterface annotation, Jav Doc says

This annotation is not a requirement for the compiler to recognize an interface as a functional interface

ddotsdot
  • 173
  • 4