What Features Can Be Mimicked with Existing Java Features?

0
10
Asked By CuriousCoder94 On

I've noticed that the community often requests certain features, yet many of them can already be closely replicated using existing functionality, making the implementation of those new features somewhat unnecessary. For example, the Concise method bodies JEP can effectively be simulated using lambdas and functional interfaces. Here's a quick illustration:

```java
void main(String[] args) {
var res = sqr.apply(2);
var ar = area.apply(2.5F, 3.8F);
var p = pow.apply(2d,2d);
}
Function sqr = a -> a * a;
BiFunction area = (length, height) -> (double) (length * height);
BiFunction pow = Math::pow;
```

While it's not as neat as defining a method like `int sqr(int a) -> a * a;`, the difference isn't that drastic. What are some other features that you think could be mimicked fairly efficiently using what we already have?

5 Answers

Answered By FunctionFocus On

I think one of the bigger frustrations I have is the naming inconsistency between Function and Action interfaces. It was nice to just use something like System.Func or System.Action without having to dig through documentation to see what they've called it this time.

ClarityLover -

Definitely! C# has a more user-friendly approach. If only Java could let us define functions in a more straightforward manner like Kotlin does!

Answered By SyntaxSleuth On

Using anonymous classes with an initializer block can give us collection literals in Java, like so:

```java
List badIdea = new ArrayList(){{ add("Bad"); add("Idea"); }};
```

It's not the best approach, but it’s a fun way to play around with syntax!

RiskyDev -

You’re a wild one! That isn’t that far from Kotlin’s scope functions, though. We should really consider expanding our syntax options!

Answered By DBInnovator On

I've been pondering the ideas from Serialization 2.0 for database interactions. The concept of having a stable wrapper with versioned records for schema migrations seems promising. It's not an immediate fix, but it'll help avoid cluttering your stack with tools like Flyway down the line.

Answered By CodeNinja2023 On

True null-safety can technically be approximated with routine null checks all over the place. It's a bit of a joke, but the idea here is valid. There are indeed gaps that can be filled by something like Jspecify for safety checks, particularly until Valhalla comes along. I’d love to see features that allow for a more explicit approach to nullability, like being able to mark types as nullable or non-nullable clearly. It could simplify our code a lot!

NullSafetyGuru -

But we need to remember that true null safety entails more than just checks. It allows optimizations at runtime and can prevent null assignments altogether, which basic checks can't achieve.

Answered By ValueClassAdvocate On

You can recreate value classes by bridging through C++ and then making bindings in Java. Sure, that’s a joke, but seriously, it’s interesting to see how libraries try to create a uniform interface while we wait for Valhalla to give us the native support.

LibraryLurker -

Some libraries already offer these uniform interfaces, but we still won’t see the performance boost until Valhalla’s features like `@ValueBased` come into play.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.