RFR: 8188055: (ref) Add Reference::refersTo predicate [v5]
Peter Levart
plevart at openjdk.java.net
Fri Oct 16 18:30:13 UTC 2020
On Wed, 14 Oct 2020 18:15:19 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:
>> For the common cases, the application should know the type of the referent and using `T` in `refersTo` will benefit
>> from the compiler type checking. For the unknown type case, cast to `Reference<Object>` is not ideal but reasonable?
>> something like this: Reference<Object> r = (Reference<Object>) ref;
>> r.refersTo(obj);
>
> That sounds reasonable to me. Thanks for looking into it.
I just want to note that if you have a `Reference<SomeType> ref` at hand, you can not just do:
Referemce<Object> r = (Reference<Object>) ref;
...since those generic types are not related. You have to do something like:
@SuppressWarnings({"unchecked", "rawtypes"})
Referemce<Object> r = (Reference) ref;
which is very unfortunate. Comparing this method with for example `Collection.contains(Object element)`, you can see
that Collection API has made a decision to not bother with T here. That was also due to keeping old code compatible
when migrating from pre-generics Java to generified Collection, but as @dfuch noted, we have a migration story here
too. We will be migrating `obj == ref.get()` to `ref.refersTo(obj)` ... Mind you that this is a boolean expression
fragment which might be written inline surrounded with other parts of expression. So you'll be forced to split that
into assignment with @SuppressWarnings and an expression or you will have to force the whole expression or method to
@SuppressWarnings. I don't know if type "safety" is forth it here.
-------------
PR: https://git.openjdk.java.net/jdk/pull/498
More information about the core-libs-dev
mailing list