[Checkers] Polymorphic Methods
Mahmood Ali
mahmood at MIT.EDU
Tue Jun 24 10:22:43 EDT 2008
Greetings,
Flowing our discussions of polymorphic qualifiers, I wanted to point
out that my proposal is overly conservative in one case. The proposal
(proposal 1) was that the polymorphic qualifier become being part of
qualifier hierarchy as supertype of all bottoms and subtype of all
supers, for the purposes of checking polymorphic methods. The
alternative (proposal 2) would be creating N methods (with resolving
the polymorphic qualifier too all possible qualifiers) and type
checking them.
It fails to see a connection between the parameters and other non-
bottom values the user introduces. Consider the following case:
@PolyNull String identity(@PolyNull String s, @Nullable String m) {
return s;
}
The previous method type checks. However the following two cases
don't type check
// case 1
@PolyNull String identity(@PolyNull String s, @Nullable String m) {
if (s == null)
return null; // doesn't type check, since null is not a
bottom qualifier
return s;
}
// case 2
@PolyNull String weirdMethod(@PolyNull String s, @Nullable String
m) {
if (s == null)
return m;
return s;
}
Both of these methods type-check under the proposal 2 but not in
proposal 1.
In my opinion, this is still consistent with Java Generics. And they
are not quite different from
// analogues to case 1
<T> T weirdIdentity(T t) {
if (t instanceof String) // notice that String is a final class
return "m";
return t;
}
// analogues to case 2
<T, E extends T> T wirdIdentity(E e, Date m) {
if (t instanceof Date) // Date isn't final
return m;
return e;
}
Needless to say, both of the cases could still work if programmer
casts the return type to (@PolyNull String).
- Mahmood
More information about the checkers
mailing list