Re: Odd behavior with type inference
On 09/19/2010 02:09 PM, Tom Anderson wrote:
it works. I can't say why, but perhaps the rules are written in terms of
inference from the types of variables, rather than expressions?
It infers expressions, but there is one case where it doesn't:
If any of the method's type arguments were not inferred from the types
of the actual arguments, they are now inferred as follows.
If the method result occurs in a context where it will be subject
to assignment conversion (?5.2) to a type S, then let R be the declared
result type of the method, and let R' = R[T1 = B(T1) ... Tn = B(Tn)]
where B(Ti) is the type inferred for Ti in the previous section, or Ti
if no type was inferred.
Most painfully, this inference does not happen if you are immediately
returning the variable, which would have been helpful in some of my code.
I would also like to point out that mixing generics and raw types is a
recipe for disaster. And then you get rare types, forever embedded in
Java because List.class is Class<List> and not Class<List<?>>.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth