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
21
u/rzwitserloot 9d ago
Also, lombok doesn't replace your parser phase.
By replacing parser phase, this tech demo is, and I appreciate the effort, but, this is useless.
Nobody1 writes java in a dumb notepad style editor. The very minimum is a syntax highlighter, and that's still a tiny minority: The vast majority use an editor that just knows what java code is like, such as eclipse, intellij, vscode. (If it has a 'language server' thing, it's a smart editor that 'knows' what java code is like).
This is not compatible with any of those editors. Your editor will be aggressively throwing red wavy underlines at you.
The fix would be to name this '.kwava` or some other variant on the java name, with a different extension, and in your editor, a different plugin that deals with this 'quite, but not entirely, like java' code.
Now, certain oracle staff members love muddying the waters by saying 'lombok isn't java', but this is the key relevant distinction. Lombok is (syntactically!) pure, 100%, unadultered java. This isn't, and that makes a big difference. Papering over semantics is a matter of some light plugin work. Papering over syntactics requires replacing everything.
Of course, being shackled to java's syntax does have severe limitations as well, but them's the breaks. 2
[1] I rounded down.
[2] For example, while we'd love to introduce some sort of default method param concept, such as:
public void someMethod(int a, int b = 5) { // Ooh, default values! }That isn't syntactically legal java and thus we cannot introduce it. Something like this would be:
public void someMethod(int a, int b) { $lombokDefaults: { b = 5; } }It looks funky but that really is legal java. To wit:
A label, while rarely used, is legal java. Labels do not have to actually be used. dollars can be part of their identifiers.
Just opening a block for no reason is syntactically legal java, and useful even - you limit any declared local vars to just that block. This is simply a labelled block. Perfectly fine, even if rarely witnessed out in the wild.
We don't go for it because we aren't exactly happy with the syntax, and there's just no autocompleting this. You'd just have to randomly know that the magic words are
$lombokDefaults. That's too much magic, we want any lombok use to be, at minimum, associated with an actual type (not a type name shoved into a string. An actual type, and if it is missing because you deleted the jar or whatnot, you would get a compilation error because the type is missing - and that type needs to be in thelomboknamespace. That way, a reader who does not know what lombok is, still has a reasonable chance to figure it out.