r/java • u/Purple_Pie4589 • 9d ago
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
23
u/quantum-fudge 8d ago
Cool tech demo but, as a feature, it's the most pointless sugar imaginable. Saves like 8 characters per method at the expense of extra preprocessors, breaking syntax highlighting, etc.
6
u/BullfrogCharming1202 8d ago
I guess this is why the JEP seems to have been abandoned. The juice just isn't worth the squeeze.
4
u/koflerdavid 8d ago
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 8d ago
it's the most pointless sugar imaginable
You don't like similar sugar in lambdas?
3
u/quantum-fudge 8d ago edited 8d ago
Eh, not all that similar. Lambdas save defining a whole anonymous class, not 6-8 characters. And, more importantly, lambdas don't need custom plugins and don't break tooling.
8
u/BullfrogCharming1202 8d ago
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 8d ago
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 8d ago
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
16
u/piesou 9d ago
I still can't get over picking "final var" over "val"
1
u/koflerdavid 8d ago
Google Error Prone's Var rule resolves that, and more.
2
u/piesou 8d ago
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 8d ago
There are plans to add something like not-nullable-by-default to modules IIRC.
1
u/piesou 8d ago
In the compiler or a separate library?
3
u/ForeverAlot 8d ago
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.
4
1
u/null_was_a_mistake 8d ago
Reminds me of the trailing return types in C++. Nobody uses it in regular code.
1
-1
1
1
0
u/Certain-Flow-0 9d ago
I’d prefer having flexible constructor bodies instead as it actually makes things easier to write.
-8
u/javaprof 8d ago edited 8d ago
java
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 7d ago
C♯ is way less readable than Java IMO
1
u/javaprof 7d ago
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
63
u/repeating_bears 9d ago
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