JEP 302: Lambda Leftovers

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Dec 7 21:49:51 UTC 2016


On 07/12/16 21:01, Roy van Rijn wrote:
> If you delete that line... nothing fails. The code runs fine, but the behavior has changed, suddenly a different 'key' is used. That is a hidden danger of shadowing. I want my code to break if declarations are removed. There should not be fallbacks to different variables?
Good point - but how is that different from this:

class Test {
    void m(String s) {
        class Foo {
             int getLength() {
                  String s = "Hello!"; //delete this
                  return s.length();
             }
        }
        System.out.println(new Foo().getLength());
    }

    public static void main(String[] args) {
        new Test().m("123"); //what does it print?
    }
}

Note that this is very similar to your example - you delete a 
declaration and suddenly you resolve to a different local variable. You 
can even construct more subtle example where you change overload 
resolution by removing a declaration in an inner class; so I wouldn't 
say it's a new issue - it mostly boils down to the (very subjective, I 
realize) perception of whether the lambda body provides a strong enough 
clue that you are inside a different scope. In my view, in most case 
this is fine - as a lambda can only declare local variable if it is a 
statement lambda with surrounding braces - so syntactic clues are in place.

Maurizio




More information about the platform-jep-discuss mailing list