Priority
Normal
Type
Feature 
State
Duplicate 
Assignee
Unassigned 
Subsystem
Editor. Editing Text 
Affected versions
Fixed in
Fixed in build Next build
Build
4155
  • Submitted by   Brian Slesinsky
    3 years ago (25 Mar 2006 23:11)
  • Updated by Brian Slesinsky
    3 years ago (13 Sep 2006 15:37)
  • Jira: IDEA-7194
    (history, comments)

IDEA-7194

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

0
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.


Environment: Any

Issue was resolved
Comments (4)
 
History (0)
 
Links
 
Eugene Vigdorchik
  Eugene Vigdorchik
26 Mar 2006 12:39
(3 years ago)
#permalink
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
  Brian Slesinsky
27 Mar 2006 09:30
(3 years ago)
#permalink
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
  Eugene Vigdorchik
27 Mar 2006 19:37
(3 years ago)
#permalink
Brian,
generic type arguments ARE inferred from return type also (see inference for Collections.emptyList() for example).
Keith Lea
  Keith Lea
27 Mar 2006 20:14
(3 years ago)
#permalink
I believe this is duplicate of, or closely related to, http://www.jetbrains.net/jira/browse/IDEABKL-2359