• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Eclipse new style for loop autocomplete?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 28717
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28717
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic