T
Timbo
Hi everyone,
A question about type inferencing in Sun's compiler: I have the
following factory method for creating an empty list:
public static <E> List<E> list()
{
return new java.util.ArrayList<E>();
}
Now, consider the following method that takes a list of String:
private void useStringList(List<String> stringList)
{ ... }
I can invoke "useStringList" on an empty list as follows:
List<String> strList = Factory.list();
useStringList(strList);
The compiler will infer from "List<String> strList" that the
return type of "Factory.list()" is "List<String>" in this
instance. However, take the following snippet:
useStringList(Factory.list());
The compiler complains that "useStringList" cannot be applied to
"List<Object>". Why is it that it can infer the type in the former
example, but not the latter? I am aware that I can write:
useStringList(Factory.<String>list());
but this is a bit comvoluted when compared to just
"Factory.list()", and there is certainly enough information to
infer the type.
Thanks,
Tim
A question about type inferencing in Sun's compiler: I have the
following factory method for creating an empty list:
public static <E> List<E> list()
{
return new java.util.ArrayList<E>();
}
Now, consider the following method that takes a list of String:
private void useStringList(List<String> stringList)
{ ... }
I can invoke "useStringList" on an empty list as follows:
List<String> strList = Factory.list();
useStringList(strList);
The compiler will infer from "List<String> strList" that the
return type of "Factory.list()" is "List<String>" in this
instance. However, take the following snippet:
useStringList(Factory.list());
The compiler complains that "useStringList" cannot be applied to
"List<Object>". Why is it that it can infer the type in the former
example, but not the latter? I am aware that I can write:
useStringList(Factory.<String>list());
but this is a bit comvoluted when compared to just
"Factory.list()", and there is certainly enough information to
infer the type.
Thanks,
Tim