bad highlighting of inferred static methods with generics

private static List ListToTypedList(Collection in) { Iterator i = in.iterator(); List out = new ArrayList(); while(i.hasNext()){ out.add((E)i.next()); } return out; } private static List buildFileList(CommandLine commandLine) { List in = ListToTypedList(commandLine.getArgList()); Hi Guys great work on the generics implimentation, but unfortunatley the last line here highlights with an error Incompatible types found: java.util.List, required java.util.List]]>

regards gary

8 comments
Comment actions Permalink

I don't know much about generics, so I could be totally wrong, but doesn't that line need to be

List in = listToTypedList(commandLine.getArgList()); ? Or maybe the ]]> goes after listToTypedList. I forget. Or, as I said, I could be wrong.

0
Comment actions Permalink

Dr Gary Thomspon wrote:

private static<E> List<E> ListToTypedList(Collection in) {
Iterator i = in.iterator();
List<E> out = new ArrayList<E>();
while(i.hasNext()){
out.add((E)i.next());
}
return out;
}
private static List<String> buildFileList(CommandLine commandLine) {
List<String> in = ListToTypedList(commandLine.getArgList());

Hi Guys great work on the generics implimentation, but unfortunatley the
last line here highlights with an error Incompatible types found:
java.util.List<E>, required java.util.List<String>

regards gary


Please submit a report to Tracker.

Friendly,
Dmitry

--
Dmitry Lomov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Is this really valid syntax? How is the compiler supposed to determine what E is in that context? I can understand that it's rather obvious in that example, but I'm sure there are many cases where it is not...

0
Comment actions Permalink

I don't know much about generics, so I could be totally wrong, but doesn't

that line need to be
>

List<String> in = <String>listToTypedList(commandLine.getArgList());


Neither do I, so I could also be totally wrong :) but I would say it should
be (as the error message says):

List in = ListToTypedList(commandLine.getArgList()); Unless the class is declared as List (or whatever the syntax is) and List]]> can be used instead, but I don't know if the
compiler handles this (and E extends String is also dubious, as String is
final).


0
Comment actions Permalink

I'm not too familiar with generics, but it looks like you're doing something like overloading based on return types. Assuming that this is not explicitly legal when using generics, you can't determine the return type until after the method runs (since you don't know what E is until you look at the Collection).

0
Comment actions Permalink

Keith Lea wrote:

Is this really valid syntax? How is the compiler supposed to determine
what E is in that context? I can understand that it's rather obvious in
that example, but I'm sure there are many cases where it is not...


Yes, it is valid according to generics-2.2. Value of E is inferred from
type of 'in' in line:

List<String> in = ListToTypedList(commandLine.getArgList());


Friendly,
Dmitry
--
Dmitry Lomov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Carlos Costa e Silva wrote:

>> I don't know much about generics, so I could be totally wrong, but
>> doesn't

that line need to be

>>
>> List in = ]]>listToTypedList(commandLine.getArgList());

The above is correct, although superflous, syntax. However, it is not
supported by IDEA at the moment.


Neither do I, so I could also be totally wrong :)

I am afraid you are :)

but I would say it
should be (as the error message says):

List<E> in = ListToTypedList(commandLine.getArgList());


E is defined only inside ListToTypedList body, so you cannot use it
here.

Friendly,
Dmitry

--
Dmitry Lomov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"


0
Comment actions Permalink

>>List in = listToTypedList(commandLine.getArgList()); > >The above is correct, although superflous, syntax. >]]>However, it is not supported by IDEA at the moment.

Actually it is not only not supported by idea but also the generics 2.2 eap compiler produces an error "illegal start of expression.

Furthermore idea 887 highlights the error (though it does pop up an internal error dialog once before doing it...)

regards gary

0

Please sign in to leave a comment.