History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEA-7194
Type: New Feature New Feature
Status: Resolved Resolved
Resolution: Duplicate
Assignee: Unassigned
Reporter: Brian Slesinsky
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
IDEA: Feedback

hide local variable types using "auto" or "var" keyword

Created: 25 Mar 06 23:11   Updated: 13 Sep 06 15:37
Component/s: Editor. Editing Text

Environment: Any

Build: 4,155


 Description  « Hide
Local type inference is appearing in mainstream languages. The next C++ standard will do it this way:

auto foo = compute_foo();

This will also be in C# 3.0, except with a different keyword:

var s = "Hello";

While waiting for Sun to get around to adding something like this to Java, maybe IDEA could have an option of automatically hiding local type definitions under a keyword? The real type could be displayed using mouse hover.

When writing new code, IntelliJ could replace the "auto" keyword with the type on the next compile.



 All   Comments   Work Log   Change History      Sort Order:
Eugene Vigdorchik - 26 Mar 06 12:39
Several considerations here:
  • you don't need to write the type of declaration in IDEA, just enter the initializer expression and introduce`variable.
  • if you still write the type of variable first, you have the opposite feature to smart complete the initializer.
  • when it comes to code readability (for example when the type contains lots of generic arguments), it could be accomplished by less aggressive language feature like allowing typedefs.
  • Java is going to have hard times adding this sort of local type inference, cause it already has one for generic methods invocations. They would either make the inference for locals not always applicable, which is bad, or delve into more global inference, that is still to be proved correct.
  • IDEA is the ide for Java, and as such it cannot understand more language constructs than language reference dictates. We could still provide respective JSR expert group with feedback regarding feature usefullness, based on the above reasoning.

Brian Slesinsky - 27 Mar 06 09:30
It seems to me that this is a small step forward from things like hiding and automatically organizing import statements. I think it would be nice if IDEA could hide and automatically manage the types of local variables too. But it is true that this is a fairly bold idea and that implementing this feature would be crossing a line.

Regarding problems with generics: I have to admit that I don't fully understand generics but it seems like this should work. As I understand it, a generic method's type is inferred from its arguments, not its return type. (If a generic method takes no arguments, you have to give it a type parameter when you call it.) Once the method's type is known, the method call's return type is known, and that can be used to deduce the type of the expression containing the method call. And, as you say, IDEA can already auto-complete a local variable's type. Also, IDEA has to determine a local variable's type when it executes the "introduce variable" refactoring, which always works as far as I know. But perhaps I am missing something?


Eugene Vigdorchik - 27 Mar 06 19:37
Brian,
generic type arguments ARE inferred from return type also (see inference for Collections.emptyList() for example).

Keith Lea - 27 Mar 06 20:14
I believe this is duplicate of, or closely related to, http://www.jetbrains.net/jira/browse/IDEABKL-2359