user1804599
user1804599

Reputation:

How can I make IntelliJ fold variable types?

Being used to languages that provide type inference (C++, Scala) I find code like this difficult to read:

ClassWriter classWriter = new ClassWriter(0);

as the type is repeated. Is there a way to make IntelliJ fold the type of the variable so that I can read and write it like this:

var classWriter = new ClassWriter(0);

but it actually stores it on disk as ClassWriter classWriter = new ClassWriter(0);?

Upvotes: 5

Views: 561

Answers (2)

MithrilTuxedo
MithrilTuxedo

Reputation: 548

The Advanced Java Folding plugin (by JetBrains) adds folding for variable declarations, among other things.

https://plugins.jetbrains.com/idea/plugin/9320-advanced-java-folding

Upvotes: 9

vikingsteve
vikingsteve

Reputation: 40388

No, you can't do that, and it's probably not a good idea for the following reason.

With java it is common to instantiate a concrete implementation type and refer to it via the abstract type.

For example:

List<String> myList = new ArrayList<String>();

In this circumstance, if you could just write var myList = new ArrayList<String>(); - what type is myList actually? Is it List<String> or ArrayList<String>?

Strong typing is central to java and the example above illustrates why.

Java 7 improves upon this somewhat, where the readability for parameterized types can use a diamond:

List<String> myList = new ArrayList<>();

In any case, I would suggest to simply try and get used to the "java way" of doing this.

Upvotes: 0

Related Questions