r/java 9h 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

24 Upvotes

22 comments sorted by

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

13

u/rzwitserloot 5h 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.

1

u/vytah 52m 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.

11

u/piesou 6h ago

I still can't get over picking "final var" over "val"

1

u/koflerdavid 5h ago

Google Error Prone's Var rule resolves that, and more.

1

u/piesou 5h ago

Thanks. This sounds similar to what you want when Nullable types release (and you'll have to mark everything explicitly as non null.

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

u/vips7L 1h ago

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.

1

u/kag0 7h ago

That IDE support will be a big one. Does it work now on editors that are LSP based?

1

u/jvjupiter 6h ago

Two of the posts are mine. 😊

1

u/revilo-1988 7h ago

Fände ich super wenn das Einzug erhalten würde

-2

u/petrifiedbeaver 4h ago

Cool demo, but dude, just use Kotlin.

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