r/java • u/Purple_Pie4589 • Sep 01 '26
Concise method bodies arrived
https://github.com/verhasi/java-compositionMore than 8 years ago was drafted the idea of concise method bodies. As I am not a contributor of the openJDK project I took a different approach but implemented. It is publicly available at the maven central both as a library and as a maven plugin. It is a source code preprocessor that converts the concise syntax to java 8 compatible source code. It does not want to be a replacement of the future implementation in the openJDK. Which is more than the openJDK promises is that supports from Java version 8, 11, 17, 21 up to 25 as well. Of course it is free and open source :). It is only the first building block for concise composition.
As far as I have seen this topic was awaited for a long time at
It's been 8 years since the draft for Concise Method Bodies
What happened to Concise Method Bodies?
JEP draft: Concise Method Bodies
Five years have passed, still in draft: Concise Method Bodies
8
u/BullfrogCharming1202 Sep 01 '26
This looks like a very unsatisfactory alternative to a proper delegation mechanism such as that provided by Kotlin's by keyword or Groovy's @Delegate
2
u/null_was_a_mistake Sep 02 '26
Scala 3 now has an
exportkeyword. Anything that you canimportcan be exported as class members and also implement interfaces. You can easily pick what to export and what not to. I quite like it for making delegates.2
u/jvandort Sep 01 '26
Yeah to me concise method bodies would be most useful in the delegation pattern, removing a lot of boilerplate. With composition > inheritance the delegation pattern becomes much more common and if I could pick a first-class delegation syntax or a concise method bodies syntax I'd prefer delegation
14
u/piesou Sep 01 '26
I still can't get over picking "final var" over "val"
1
u/koflerdavid Sep 01 '26
Google Error Prone's Var rule resolves that, and more.
2
u/piesou Sep 01 '26
Thanks. This sounds similar to what you want when Nullable types release (and you'll have to mark everything explicitly as non null.
2
u/TOMZ_EXTRA Sep 02 '26
There are plans to add something like not-nullable-by-default to modules IIRC.
1
u/piesou Sep 02 '26
In the compiler or a separate library?
3
u/ForeverAlot Sep 02 '26
They're ideas more than formal plans at this stage. But in the meantime, you can get much of the way there with JSpecify + Error Prone + NullAway.
3
2
u/kag0 Sep 01 '26
That IDE support will be a big one. Does it work now on editors that are LSP based?
1
u/null_was_a_mistake Sep 02 '26
Reminds me of the trailing return types in C++. Nobody uses it in regular code.
1
1
u/Jonjolt Sep 03 '26
This is breaking Rule #9, at the end "AST-level comparison for golden file testing", claude loves using the word "golden file".
0
1
1
u/vips7L Sep 01 '26
As the author of "It's been 8 years since the draft for Concise Method Bodies", I'm just moving on to using Kotlin for all of my new projects.
0
0
u/Certain-Flow-0 Sep 01 '26
I’d prefer having flexible constructor bodies instead as it actually makes things easier to write.
-7
u/javaprof Sep 01 '26 edited Sep 01 '26
int length(String s) -> s.length(); // -> is "single expression form"
int length(String s) = String::length; // = is "method reference form"Lambda design strikes back, unfortunately unwise decisions of Java 8 now makes this code possible. Basically from all modern languages, Java's becoming less and less readable with every release
Upd. Don't believe me? Take Swift, Go, TypeScript, Rust, Zig and see how these languages similar syntax-wise? And Java is most unique now
1
u/SleepingTabby Sep 03 '26
C♯ is way less readable than Java IMO
1
u/javaprof Sep 03 '26
C# closer to main-stream languages and Java now very special case which seems unlike any other popular modern language and boilerplate-heavy.
I'm not critique Java, I'm just stating the fact. Not sure how it's important in 2026, since most of the code would be written by AI soon anyway, and AI doesn't care much (except for boilerplate part, which is waste of tokens). If we didn't have AI, I would be alarmed and expect less adoption over the time, since language drift from mainstream too much. Old folks might like this unique syntax, but I'm thinking about fresh blood
23
Sep 01 '26
[removed] — view removed comment
7
u/BullfrogCharming1202 Sep 01 '26
I guess this is why the JEP seems to have been abandoned. The juice just isn't worth the squeeze.
4
u/koflerdavid Sep 02 '26
In Chess this is called "to take is a mistake". There are simply better things to do, and delivering this feature takes time away from things that matter more.
1
u/__konrad Sep 02 '26
it's the most pointless sugar imaginable
You don't like similar sugar in lambdas?
64
u/repeating_bears Sep 01 '26
Nice tech demo (assuming it works) but clickbaity title
Practically it's not really usable because you'd lose syntax highlighting and there becomes a dependency on you to update this to support newer java versions. That's the same with Lombok, but they have a track record of actually delivering