Impossible to reorganize methods with underscore as parameter in lambda?

I have a ton of classes with one method (half of them do not use their parameter and therefore calls it underscore), and I just switched to Java 8. Now I want to replace them with lambdas.

Here is my code:

interface I {
  void stuffHappened(Integer howManyStuff);
}

public class A {
  void go() {
    I i = new I() {
      public void stuffHappened(Integer _) {
        System.out.println("woah!!");
      }
    };
  }
}

And after:

interface I {
  void stuffHappened(Integer howManyStuff);
}

public class A {
  void go() {
    I i = _ -> System.out.println("woah!!");
  }
}

As you can see, this seems to be no longer valid, claiming that using "_" as the parameter name is no longer valid. But why did this allow this refactor in the first place? And why does this work with anonymous classes, but not with lambdas? Is this a really new rule? Or is this some kind of inconsistency in IDEA?

Intellij-IDEA ( ), , , Android Studio (Beta) 0.8.1 ( retrolambda, lambdas Android), , , IDEA.

+4
2

Lambdas _ , .

JLS 15.27.1. :

, _ ( ).

_ . Java / .

__, $, , unicode, _ (, , - - , ).

+4

Java 8 _ . Java 8, , - , . . .

(pre-lambda) , . , " ".

_ , . , _, ! IntelliJ IDEA , , .

, , IDEA.

+7

Source: https://habr.com/ru/post/1548436/


All Articles