JEP draft: Concise Method Bodies - extend this to local functions?

Lukas Eder lukas.eder at gmail.com
Wed Sep 19 20:44:04 UTC 2018


Hello,

I've just seen this new JEP draft, which really looks very useful:
http://openjdk.java.net/jeps/8209434

Now, given that this would be possible:

    int x(String s) -> s.length();

And given that this almost looks like a named lambda expression, could this
maybe be an occasion to re-discuss the possibility of supporting named
lambda expression / local functions?

E.g.

    void m() {

        // lambda local to m()
        ToIntFunction<String> x1 = (String s) -> s.length();

        // "method" local to m()
        int x2(String s) -> s.length();

        // We can now do:
        int i1 = x1.apply("abc");

        // More concise
        int i2 = x2("abc");
    }

I know this was discussed for Java 8 and rejected, but given the high
similarity of the currently proposed concise method body syntax and the
lambda syntax, I feel that on a high level, this might appear coherent and
useful.

Of course, in such a case, it would appear useful to allow the original
method body syntax as well in a local method:

    void m() {
        int x3(String s) {
            return s.length();
        }
    }

Also, many other languages have this feature, e.g. Scala, Ceylon, Kotlin

Thanks,
Lukas


More information about the amber-spec-comments mailing list