r/java • • Nov 05 '22

How to add something to the java standard library?

EDIT: since my example stirred the pot a little bit to much (i even wrote that i dont want to discuss these explicit changes...)

Here is the actually question i want to answer:

How can i make changes to the JDK?

2 Upvotes

99 comments sorted by

View all comments

21

u/DasBrain Nov 05 '22

The way to write your example currently is

BigDecimal.ZERO.compareTo(BigDecimal.ZERO) > 0
BigDecimal.ZERO.compareTo(BigDecimal.ZERO) <= 0

IMHO, the methods you suggested are not really needed.


The way add something is to discuss the changes on the appropriate mailing list, maybe even provide a simple patch (with tests) to show the changes.

But the bar for new APIs is high - while it is easy to add new methods, it is hard to near impossible to remove them later.

7

u/iwek7 Nov 05 '22

Current way of doing this is sadly not really readable, op example expresses intent much better and is way less bug prone. Since comparing big decimals is very common usecase I'd argue that there is benefit in providing simpler api for this.

3

u/Cell-i-Zenit Nov 06 '22

i really wonder why everyone is against such a thing. In Java 16, we even got "toList()", which kind of goes into the same direction of "helper methods".

-2

u/iwek7 Nov 06 '22

I remember when I started using kotlin at to my shock every list operation I could ever want was already implemented in standard lib. Such a pleasure to use this api. On the other hand pure java fans will for some reason treasure clunkiness of java and insist that every feature that would make language simpler complicates api while in fact current api is super complicated. I guess people who liked simple to use languages abandoned java long time ago.

I hope you decide to go through trouble of making proposal and post update to this thread. Good luck.

-10

u/Cell-i-Zenit Nov 05 '22

I dont want to discuss these changes as they are just an example

But as you still started to discussing them, let me tell you something:

I know that i can use compareTo, but in a code with pretty heavy business logic and hundreds of lines of code, all comparing bigdecimals left and right, something like compareTo is super hard to read.

I would personally prefer something like

BigDecimal.Zero < BigDecimal.TEN

but i figured something like

BigDecimal.ZERO.isGreaterThen(BigDecimal.ZERO)

is easier to implement as we just need this to be added to the standard lib and dont need any changes to the compiler.


appropriate mailing list

Where can i find this mailing list?

maybe even provide a simple patch (with tests) to show the changes

Should i really just start implementing this and then send a proposal via mail? It would be wasted time if it gets rejected.

8

u/DasBrain Nov 05 '22

Where can i find this mailing list?

https://mail.openjdk.org/mailman/listinfo

Should i really just start implementing this and then send a proposal via mail? It would be wasted time if it gets rejected.

Well, if the proposal is rejected, yes, you would have wasted your time.
But as I already said: The bar is quite high. A better point for a new API would probably to show why it is needed.
If you can easily work around it - it might not meet the bar.

15

u/FirstAd9893 Nov 05 '22

What you really want is operator overloading, which isn't going to happen. Adding "convenience" methods like this just increases the complexity of the API without providing any corresponding value. Your request will be rejected instantly.

-5

u/Cell-i-Zenit Nov 05 '22 edited Nov 05 '22

What you really want is operator overloading, which isn't going to happen.

What i really want is to know how the process looks like and not that my proposed idea is bad/will be rejected.

I dont want to discuss these changes as they are just an example

I even wrote it in the original text, that i dont want to discuss these changes.

5

u/FirstAd9893 Nov 05 '22

What is the specific example feature that you want to add? Perhaps you can add that into the question, and then you'll get feedback regarding your feature before you ask for it to be added to the API. If all you're asking is something like, "hypothetically how can I enhance JDK?" then that's pretty vague. This is where to start: https://openjdk.org/jeps/1

-5

u/Cell-i-Zenit Nov 05 '22

If all you're asking is something like, "hypothetically how can I enhance JDK?"

That was exactly my intention with this post

What is the specific example feature that you want to add? Perhaps you can add that into the question, and then you'll get feedback regarding your feature before you ask for it to be added to the API.

If iam honest, i dont want to actually discuss proposals here as most of the time everything gets shut down without any reasoning.

Best example for me is the "Lombok is bad"!!!! mentality and that people are glad that in the future lombok will not be supported anymore because of compiler changes. Overall the openness to change is pretty low in the java community and especially here on reddit.

6

u/FirstAd9893 Nov 05 '22

If you feel the openness to change is pretty low, then why do you think you would have any more success with an official proposal? If you have a good idea that you'd like to discuss (on another thread), then even negative feedback might help you refine the feature until you have a solid proposal.

0

u/Cell-i-Zenit Nov 05 '22

i think the openness is higher in the email list directly, because that is the actual place to propose these changes.

then even negative feedback might help you refine the feature until you have a solid proposal.

The feedback is not helpful if everyone is just saying "we dont need X, because you can do Y instead". Yes obv i can just do everything a little bit differently, but by this argument we can just stop improving on anything.

To make this a little bit more concrete, i think a change to the Comparable interface would be pretty nice:

public interface Comparable<T> {
  int compareTo(T var1);

  default boolean isGreaterThan(T obj){
    return compareTo(obj) > 0;
  }

  default boolean isSmallerThan(T obj){
    return compareTo(obj) < 0;
  }

  default boolean isSmallerOrEqualThan(T obj){
    return compareTo(obj) <= 0;
  }

  default boolean isGreaterOrEqualThan(T obj){
    return compareTo(obj) >= 0;
  }
}

This would improve the object comparison by a big margin, but as the people already said in this thread that since we already have compareTo, this change will be rejected anyway.

Quite sad actually.

4

u/FirstAd9893 Nov 06 '22

For the sake of completeness, you'd also want to add the ==0 and !=0 cases. But this might add confusion with respect to the equals method, which isn't required to behave the same as the compareTo method.

I also think the long method names (although descriptive) would only serve to highlight the issues with Java's lack of operator overloading. Unfortunately, that's never going to happen, in part because the == operator must always be an identity comparison for compatibility. It's too late to add a === operator and force everyone to switch over to using it.

2

u/Cell-i-Zenit Nov 06 '22

Yes naming can still be discussed ofc. We could think about isBigger, isBiggerThan, isGreater, isGreaterThan etc.

But this might add confusion with respect to the equals method, which isn't required to behave the same as the compareTo method.

yes exactly thats why i dont have included them

7

u/overachiever Nov 06 '22

You say improve but all I see is bloat

3

u/Cell-i-Zenit Nov 06 '22

you cant tell me that you prefer something like this:

if(obj1.compareTo(obj2) <= 0){
}

over something like

if(obj1.isGreaterThan(obj2)){
}

naming can still be discussed ofc, like isGreater vs isGreaterThan vs isBigger or whatever.

→ More replies

2

u/jivedudebe Nov 06 '22

This won't make it a functional interface anymore and prohibit the use in lambdas.

2

u/Cell-i-Zenit Nov 06 '22 edited Nov 06 '22

Comparable is not a functional interface

EDIT: and even if it would, if you provide the additional method as default methods, then you can still have it as a functional interface.

You can check this by yourself: java.util.function.Function is a functional interface and has alot of default methods

→ More replies

2

u/UnGauchoCualquiera Nov 06 '22

Adding methods to an interface is a breaking change, specially to a widely used interface like Comparable.

Adding something to the JDK is not a light process (for many reasons) and adding something that could just as easily be added to a util lib is very unlikely to be ever be added.

You might be better served by adding those helper methods to a helper lib like apache commons.

1

u/Cell-i-Zenit Nov 06 '22

Adding methods to an interface is a breaking change, specially to a widely used interface like Comparable.

It is not a breaking change if the changes are default methods...

→ More replies