News for hackers. Stuff that matters.
Re:Fluent: Static Extension Methods for Java by jillesvangurp on Monday July 03, 2023 @03:31AM (#36569916)
Nice, this is one of the features that makes using Kotlin with existing Java frameworks so nice. It can really cut down on the boiler plate Java is infamous for. You can define extension functions on any type, or type alias. So anything that looks like it could be shorter or is a bit repetitive, you just define an extension function to make that go away.
Re:Fluent: Static Extension Methods for Java by misja111 on Monday July 03, 2023 @03:36AM (#36569955)
How does this cut down on boilerplate? The examples given on the website seem equally long.

assertNotEmpty(getHttpContent(createSharedUrl(website, "styles.css")));

vs

website.createSharedUrl("styles.css").getHttpContent().assertNotEmpty();

Re:Fluent: Static Extension Methods for Java by tuyiown on Monday July 03, 2023 @04:18AM (#36570224)
Typically, boiler plate implemented by a standalone function is not easily discoverable, eg. no clear convention where to put it, does not look idiomatic on some usages syntax habits, and not suggested by auto completion in top results.

Extensions are searchable by syntax and naturally in a namespace, largely diminishing the risk of put it in in a single use function that will be quickly forgotten, and might seem obscure.

The code itself might not be much shorter, but you don't introduce code looking weird, if it's re-usable it's now discoverable, and if your boilerplate contained a lot of micro adjustments on a larger glue code, this glue code is now shorter and way clearer.

Re:Fluent: Static Extension Methods for Java by misja111 on Monday July 03, 2023 @04:38AM (#36570341)
> Extensions are searchable by syntax

This would be very useful. But how are these extension methods more searchable by syntax than the 'old fashioned' static util methods? I admit I haven't tried, but I suspect that code completion in my IDE wouldn't include these extension methods.

Re:Fluent: Static Extension Methods for Java by sjrd on Monday July 03, 2023 @05:13AM (#36570570)
At least in Scala, code completion in the IDE does include available extension methods.
Re:Fluent: Static Extension Methods for Java by jdmichal on Monday July 03, 2023 @12:17PM (#36575095)
They're more discoverable, because typing in `object.` tells the IDE that you are looking for methods on the type of `object`. If the IDE is aware of extension methods, it can also expose them the same as instance methods.

Java specific: If you're looking for a static utility method, then `UtilClass.` will also autocomplete. However, what if the function you're looking for isn't in `UtilClass`, but `SomeOtherClass`? What if you don't even know whether a method already exists for what you're trying to accomplish, and it could possibly exist in multiple libraries? Your discoverability engine quickly becomes Google.

Re:Fluent: Static Extension Methods for Java by rogerkeays on Monday July 03, 2023 @04:28AM (#36570280)
Also, creating a subclass just to add extension methods is definitely boiler plate. Util classes too, though they are useful for packaging functions at least.
Re:Fluent: Static Extension Methods for Java by kaba0 on Monday July 03, 2023 @06:15AM (#36570953)
Subclassing may not always be possible (final class, or method) and can break class invariants. Util classes with helper methods and/or extension methods are not prone to these problems.
Re:Fluent: Static Extension Methods for Java by rogerkeays on Monday July 03, 2023 @09:11AM (#36572392)
Yes, inheritance is a nightmare!
Re:Fluent: Static Extension Methods for Java by jillesvangurp on Monday July 03, 2023 @06:45AM (#36571139)
By extracting repetitive bits of code and reducing them to a simple function call.

The website examples aren't very inspiring indeed. Have a look at some of the extension functions for the Collections interface:

https://github.com/JetBrains/kotlin/blob/master/libraries/st...

Re:Fluent: Static Extension Methods for Java by MrBuddyCasino on Monday July 03, 2023 @03:35AM (#36569952)
When you start transforming the AST, that might be a sign you should switch languages. Kotlin is nice, why not use it.
Re:Fluent: Static Extension Methods for Java by rogerkeays on Monday July 03, 2023 @03:48AM (#36570037)
Kotlin is great, but it is a fairly heavy dependency if all you want is extension methods. Java has mostly caught up in many other aspects, so I'm experimenting with Java + one or two compiler hacks. Checked exceptions are my next target...
Re:Fluent: Static Extension Methods for Java by javanonymous on Monday July 03, 2023 @06:48AM (#36571161)
I'm curious about how you plan to work on checked exceptions.

If it's just about ignoring forced exception management, Lombok provides the @SneakyThrows annotation.

I would need something more before I add a new type of extension. Maybe if you could solve the null-check issue.

i.e. rewriting

    var x = a.getFoo()?.getBar()
to

    var x = a.getFoo() != null ? a.getFoo().getBar() : null
In that case I may consider it for my personal projects, and then potentially recommend it at work once it becomes more widespread and has IDE support.
Re:Fluent: Static Extension Methods for Java by rogerkeays on Monday July 03, 2023 @09:14AM (#36572433)
I don't know yet. I'm already using some of my own variations of sneakyThrows, but what I really want is just to disable the whole damn thing in the compiler (checked exceptions are a compiler feature, not a JVM feature). I won't be changing the Java syntax though, sorry!
Re:Fluent: Static Extension Methods for Java by zeedude on Monday July 03, 2023 @09:57AM (#36572934)
Re:Fluent: Static Extension Methods for Java by rogerkeays on Monday July 03, 2023 @10:27AM (#36573391)
sweet
Re:Fluent: Static Extension Methods for Java by kaba0 on Monday July 03, 2023 @06:23AM (#36571017)
I think Kotlin differs far too much from Java — I don’t want all those plus features that will inevitably branch too far from the JVM/Java corresponding code, becoming its new thing.

I might want a nearly-Java language that only adds trivial syntax sugar, but are otherwise the same. Groovy sort of had a good initial idea here (being a superset of Java, though again, it has grown apart a bit since).

For me Kotlin lives in this weird limbo between these two extremes. If I want a different language on the JVM I would rather do Scala or Clojure.

Re:Fluent: Static Extension Methods for Java by vips7L on Monday July 03, 2023 @09:53AM (#36572892)
For me, if Kotlin is on the table I might as just use C# where the entire ecosystem is built around these concepts.
Re:Fluent: Static Extension Methods for Java by kaba0 on Monday July 03, 2023 @10:11AM (#36573123)
I wouldn’t really leave behind the JVM ecosystem, which is much bigger and better quality than the .NET one from what I gathered (though it is still good and bigger than most other languages’). And other people’s code is the numero uno productivity boost, so it’s an important area.
Re:Fluent: Static Extension Methods for Java by vips7L on Monday July 03, 2023 @01:26PM (#36576174)
I think I would be fine with a smaller ecosystem. I've seen a lot of projects that have made bad choices in libraries because there are too many. Sometimes I feel like if there was just one blessed jdk way it would be a lot simpler. Honestly, the only thing that is keeping me from .NET right now is async/await. Pron really did Java a service by pushing for virtual threads.