Community
Participate
Working Groups
Given the code: ArrayList<T> list = new ArrayList<T>(); for (item : list) { // error } There is an error on "item", as it is undeclared. Quickfix asks if you want to create a class, interface or enum for "item", or add a type parameter "item" to the class. I would expect a suggestion of declaring an iterator variable "T item". (Also the compiler is confused because it says on "list" that "Cannot convert element type T to item", so it thinks item is a type.) This looks like a trivial content-assist but saves a lot of typing when iterating through complex generic containers-inside-containers.
Its actually a recovered syntax error (type or name missing). We could detect that case. Does it happen a lot?
Yes, I hit this *all* the time... mostly because I'm lazy about filling in types (they are tedious and automatically-deducable), so when I declare a variable I just do "x = value();" or something then use Eclipse Quickfix to fill in the type declaration. That doesn't work with foreach loops. (I wish Java had a "var x = value()" syntax like Javascript...) It's worth it when you have types like ArrayList<ArrayList<ConcurrentHashMap<Integer,String>>>... typing in types like that all over the place is a real pain.
Current behavior as of Eclipse downloaded today (Helios SR-2, 20110218-0911) is almost the same as when reported in 2006. Both "item" and "list" in the example above are underlined in red. Selecting Quick Fix on "item" gives: - Create class/interface "item" - Change to "<similar name>" (<package>) - Create enum "item" - Add type parameter "item" to <EnclosingClass>/<enclosingFunction()> ...none of which are helpful. Selecting Quick Fix on "list" gives: - Change type of '$missing$' to 'ArrayList<T>' (I think this was added in about 2007.) Selecting this last option gives the following code and leaves the cursor at position '|': ArrayList<T> list = new ArrayList<T>(); for (ArrayList<T> : |list) { // error } i.e. "item" is completely eliminated.
I agree with Luke, this would be very helpful for me. I use quickfix to add type declarations all the time and it's really annoying that it doesn't work in foreach loops.
There are other quickfix options that are available for standard for loops too, but not for foreach loops, for example converting the statement after the for loop into a block or vice versa.
Yes, we should fix that for 3.8. The AST already contains all the information we need. Manual workaround is to insert e.g. 'x ' in front of 'item' and then use the quick fix on 'list' to change the type of 'item'.
I use the 'x' fix all the time now.. Makes me think Eclipse should implement a "var" type as some sort of "dynamic macro" that is expanded into the correct type as soon as you leave the line.
*** Bug 139547 has been marked as a duplicate of this bug. ***
Created attachment 217226 [details] quick fix v0.1 Here's what I did until I realized that most of this should be fixed in the compiler first.
This is quite annoying, so I went forward without a fix for bug 180456. This fix needs to be revised when bug 180456 gets fixed. http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=9f57afcb99ecfb6b3aa663ebe978b051fb75c842
Verified in I20130430-2000.