JEP draft: Concise Method Bodies - extend this to local functions?
Lukas Eder
lukas.eder at gmail.com
Thu Sep 20 07:05:00 UTC 2018
Hi Brian,
I absolutely agree that there is no reason to mix the two features. The
only reason I brought it up is because I found the currently proposed
syntax for concise method bodies so similar to lambdas (which is great),
which are often abused as poor man's local methods.
I also find the idea of the general cleanup of nesting constraints *very*
interesting.
Thanks,
Lukas
On Thu, Sep 20, 2018 at 12:23 AM Brian Goetz <brian.goetz at oracle.com> wrote:
> This was received from the suggestion box …
>
>
> My thoughts on this: local functions are a potentially useful feature, and
> one that interacts nicely with the concise form, but the two features are
> orthogonal; there’s nothing to be gained by doing them together that is not
> gained by doing them sequentially in either order. While I’ve got nothing
> against local functions, I would rather do local functions in the context
> of a more general cleanup about nesting constraints (e.g., restrictions on
> static fields of inner classes, private classes nested in interfaces, local
> interfaces and enums, etc.) There’s a whole pile of “it makes sense to
> nest X in Y, but you can’t” irregularities that can be cleaned up at once —
> but I think there’s more immediate payback in doing the more constrained
> feature of concise method bodies first.
>
>
> > Begin forwarded message:
> >
> > From: Lukas Eder <lukas.eder at gmail.com>
> > Subject: JEP draft: Concise Method Bodies - extend this to local
> functions?
> > Date: September 19, 2018 at 4:44:04 PM EDT
> > To: amber-spec-comments at openjdk.java.net
> >
> > 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-observers
mailing list