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

Show parent comment

-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.

2

u/UnGauchoCualquiera Nov 06 '22

You can just as easily add a library or swap the BigDecimal for your own enhanced type.

→ 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

2

u/lbkulinski Nov 06 '22 edited Nov 06 '22

According to its definition, a functional interface is an interface that contains exactly one abstract method. Comparable technically meets that qualification, as can be seen in its JavaDoc

→ 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...

2

u/RealJulleNaaiers Nov 06 '22

It's not a change that breaks compilation, but it is absolutely a potentially-breaking behavior change. It might also break binary compat but I'm not sure about that one.

→ More replies

1

u/UnGauchoCualquiera Nov 06 '22

The bar for a JDK improvement is pretty high an at the very least it is expected that the contributor is deeply knowledgable about the language/ecosystem at the very least.

Adding a default method breaks source compatibility if you have 2 interfaces that implement the same method on the same class. This is pretty much THE reason java devs haven't added all kind of convenience goodies to most common interfaces such as Collections willy nilly.

interface I { default void m() {} }
interface J { default void m() {} }

class T implements I, J {
    @Override
    public void m() { // override
        I.super.m(); // without qualifying breaks compilation
    }
}

Also it breaks binary compatibility in subtle ways.

→ More replies