r/java • u/Purple_Pie4589 • 9h 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
11
3
9
u/quantum-fudge 6h 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.
2
u/BullfrogCharming1202 3h ago
I guess this is why the JEP seems to have been abandoned. The juice just isn't worth the squeeze.
4
u/BullfrogCharming1202 6h 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
3
u/jvandort 5h 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
1
1
-2
0
u/Certain-Flow-0 6h ago
I’d prefer having flexible constructor bodies instead as it actually makes things easier to write.
-4
u/javaprof 5h ago edited 1h 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
43
u/repeating_bears 7h 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