Method argument inference?

John Hendrikx hjohn at xs4all.nl
Sun Sep 20 02:00:22 PDT 2009


I'm wondering if there's anything upcoming for Java 7 that's gonna fix 
this problem:

  dealWithStringSet(Collections.emptySet());

Where the method is defined as:

  public void dealWithStringSet(Set<String> strings);

This gets flagged as a warning by the compiler, as it cannot infer the 
type from the method parameters.  The code below is warning free however 
with an unnecessary intermediate assignment:

  Set<String> emptySet = Collections.emptySet();
  dealWithStringSet(emptySet);

The question is, why is it being flagged at all?  Collections.empySet() 
is clearly setup to conform to any generic type you want it to be, and 
there's no danger (that I can tell) in doing the same inference for 
methods as it is not allowed to write two methods anyway with the same 
erasures. 

Even if it is allowed later to write two methods that only differ in 
generic parameters, one could apply the same error as is used for null 
values (where you have to cast null to conform to the intended type).

It is just a minor annoyance, but one that gets more frequent as more 
and more frameworks and methods adopt generics :)

--John





More information about the coin-dev mailing list