This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes IDEs, Version Control and other tools and the fly likes Eclipse new style for loop autocomplete? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Eclipse new style for loop autocomplete?" Watch "Eclipse new style for loop autocomplete?" New topic
Author

Eclipse new style for loop autocomplete?

Xolani Nkosi
Ranch Hand

Joined: Apr 29, 2009
Posts: 32
Something that's been bugging me for a while. Why can't Eclipse infer the object type of a collection when used with new style for loops? e.g.

CTRL-1 on that for loop gives me the options to:

Create class 'something'
Create interface 'something'
Change to SomeRandomClassThatSoundsLikeSomething (com.somepackage)
... lots of these
etc.

Where is the most obvious fix (create local variable 'something'), which is the first suggestion if you do:

Or am I missing it?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you auto-complete the "for" instead of typing all that in, it does; it'll also try to choose the most likely iterable object. Since it's expecting a type, not a variable name, what it's doing seems reasonable.
Xolani Nkosi
Ranch Hand

Joined: Apr 29, 2009
Posts: 32
David Newton wrote:If you auto-complete the "for" instead of typing all that in, it does; it'll also try to choose the most likely iterable object. Since it's expecting a type, not a variable name, what it's doing seems reasonable.

It's not particularly reasonable, as 99 times out of a hundred, if I write for (a : b) I'm expecting a to be the reference to each item in collection b, and for it to therefore autocomplete the type for me. I didn't really want to argue about whether what it does is right or not, but rather if anyone had a solution to make it work the way I'd like (and logically expect) it to.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It won't do that unless you enhance the completion code to guess that you're putting in a variable name instead of a type. Just because *you're* expecting it to be able to guess that the variable name isn't actually a type name and you've entered the thing that comes *next* in the token stream... well, I dunno. Why not just just the feature it *does* has, which involves a lot less typing anyway?
Xolani Nkosi
Ranch Hand

Joined: Apr 29, 2009
Posts: 32
David Newton wrote:It won't do that unless you enhance the completion code to guess that you're putting in a variable name instead of a type. Just because *you're* expecting it to be able to guess that the variable name isn't actually a type name and you've entered the thing that comes *next* in the token stream... well, I dunno. Why not just just the feature it *does* has, which involves a lot less typing anyway?

Because the principle of least surprise and good interface design means it should behave in the most expected way. Which is as I described. Like if I write:

and CTRL-1 on it, it correctly assumes I want to make a local called a, and offers to fill in the type returned from someMethod(). It doesn't assume that a is a class. So my point is, why is the enhanced for loop not the same? Seems broken.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd actually be *more* surprised if it did what you wanted, but meh.

In any case, your best bet would be to file an enhancement request against Eclipse; there's no way to make it do what you're asking out-of-the-box. Although I still don't understand why you don't just use the "for" autocomplete.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I also use the foreach autocomplete feature. I wouldn't use the feature you are trying to achieve. That means typing "for ( a : b )" and going back to "a" for Quick Fix. Mmmm. Faster to type "for" and autocomplete. Faster to enter the type in the first place But it doesn't hurt to send a request at the Eclipse forum, as David suggested you. Some people may wish it too.


[My Blog]
All roads lead to JavaRanch
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15961
    
  19

Is that actually even legal? I thought the "foreach" construct declared the variable within the scope of the loop. The collection equivalent of:


Which, assuming the compiler doesn't whine, is actually going to define 2 different "i";s - one in the scope of the loop, and the method-scope i, which will be hidden for the scope of the loop.

CTRL-1 is an error correction mechanism, not a code helper, per se. It's a small, but critical difference.


Customer surveys are for companies who didn't pay proper attention to begin with.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's not legal, I think the OP is suggesting that Eclipse should include auto-insertion of the type as one of the available fixes (although I'm not sure how it would understand the mistake wasn't a missing type, which is what it does).
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15961
    
  19

So much for the new eyeglasses.

Actually, it's reasonable for the IDE to look back at the referenced collection ("something") and supply the missing data type ("SomeThing").

What threw me was the mention that it should do a declaration. The "for (something" already is a declaration. While the lack of capitalization isn't a foolproof hint that "something" is an object missing a type and not type whose name defies recommended capitalization rules, the fact that it's not a subclass of Class or Interface should be enough. I think.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Eclipse new style for loop autocomplete?
 
Similar Threads
Initialization
How to create master/detail form like access or oracle did?
Lomboz not creating XDoclet code
Eclipse: can I override ${user}?
how to set classpath if i place properties file in default/data directory