r/java 16h ago

Concise method bodies arrived

https://github.com/verhasi/java-composition

More 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

31 Upvotes

25 comments sorted by

View all comments

53

u/repeating_bears 13h 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

14

u/rzwitserloot 12h 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:

  1. A label, while rarely used, is legal java. Labels do not have to actually be used. dollars can be part of their identifiers.

  2. 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 the lombok namespace. That way, a reader who does not know what lombok is, still has a reasonable chance to figure it out.

2

u/vytah 7h ago

This is simply a labelled block. Perfectly fine, even if rarely witnessed out in the wild.

Labelled blocks are useful, as you can break out of them, which is the closest to goto you can get.