On Fri, 16 Oct 2020 19:22:16 GMT, Mandy Chung <[email protected]> wrote:
>> 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.
>
> Reference instances should not be leaked and so I don't see very common that
> caller of `Reference::get` does not know the referent's type. It also
> depends on the `refersTo` check against `null` vs an object. Any known use
> case would be helpful if any (some existing code that wants to call
> `refersTo` to compare a `Reference` of raw type with an object of unknown
> type).
>
> FWIW, when converting a few use of `Reference::get` to `refersTo` in JDK,
> there is only one case (`equals(Object o)` method that needs the cast.
>
> http://cr.openjdk.java.net/~mchung/jdk15/webrevs/8188055/jdk-use-refersTo/index.html
@mlchung I don't have many known use cases, but how about
WeakHashMap.containsKey(Object key) for example? Currently
`WeakHashMap.Entry<K, V> extends WeakReference<Object>` but it would be more
type safe if it extended `WeakReference<K>`. In that case an
`entry.refersTo(key)` would not work...
-------------
PR: https://git.openjdk.java.net/jdk/pull/498