Why does LongProperty implement the <Number> property, but not the <Long> property?

I came across what looks like a feature of the JavaFX API: LongProperty implements Property<Number> but not Property<Long> .

What is the reason for this? I kind of understand that all this is connected with the inherent problem of Java with covariance and contravariance, because generics, where it is implemented stupidly through erasure, maintain backward compatibility with bytecode; but what problem could arise if LongProperty implemented both Property<Number> and Property<Long> ?

Edit: This question arose because of this problem: Programmatically apply LongProperty to TableColumn (vs semantically)

+2
source share
1 answer

He cannot realize both.

To do this, he will need to implement two versions of each method in an interface that uses a common one. Take as an example:

 bindBidirectional(Property<Long> other) { ... } 

Under the hood, erasing means it compiled to:

 bindBidirectional(Property other) { ... } 

So, what would something do that implements Property<Number> and Property<Long> ? It will have two methods:

 bindBidirectional(Property<Long> other) { ... } bindBidirectional(Property<Number> other) { ... } 

... which will be compiled after erasing in two ways:

 bindBidirectional(Property other) { ... } bindBidirectional(Property other) { ... } 

These two methods conflict, and there will be no way to resolve them at run time.

Even if you used some kind of compiler trickery to get around this, what happens when someone uses LongProperty as a raw property?

 Property rawLongProperty = new LongProperty(); rawLongProperty.bindBidirectional(someOtherRawProperty); 

It is not possible to know which of the two options bindDirectional intended to solve.

+4
source

Source: https://habr.com/ru/post/1240163/


All Articles