Question

I wanted to check Java 8 integration with Eclipse Luna so I downloaded the M7 Luna from eclipse.org.

After configuring the JDK to jdk8u5, I started some tests.

Let's say you have a nice Runnable like

Runnable r = new Runnable() {
   @Override
    public void run() {
        System.out.println("foo");
    }
};

If you select the

new Runnable() {
  @Override
  public void run() {
    System.out.println("foo");
  }
}

block and press Ctrl-1 (Quick Fix), you get the suggestion to change it to a lambda, resulting in Runnable r = () -> System.out.println("foo");, which is pretty cool.

But a nicer thing whould be to actually help creating lambda expression. For instance, if you type Runnable r = | (with | being the cursor location) and press ctrl+Space (content-assist), I would have expected to find a "create a lambda expression from this functional interface" option in the displayed popup. But nothing new is available.

Do you know if this will be implemented in the future ? I think it might have something to do with the templates (Java/Editor/Templates in preferences) but I actually never experimented with them.

Was it helpful?

Solution

Providing good proposal right after the = is rather tricky as almost everything could be placed on the right hand side of an assignment.

Even the old way of implementing a function using an anonymous inner class was not proposed right after the equal sign. You had to type the four characters new␣ before the suggestion came up. And four characters is exactly what you have to type to create a lambda, ()->, but at this place proposing the creation of a lambda makes no sense anymore as you have already created it.

So proposing a lambda would require lifting its priority compared to other proposals to appear right after the equal sign but it would still have rather limited benefit. You had to press crtl+space unless you use automatic menu popup, then select “create lambda” to just get either the four characters ()-> or something like name-> inserted whereas the parameter name(s) are likely to be changed after the proposal is inserted.

For an inner class, read method overriding, it makes sense to propose parameters as you have to repeat all parameter types exactly, but for a lambda where you can omit all the bulk the saving is very limited.

So I don’t expect a proposal of lambda creation to ever appear in the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top