简体   繁体   English

如何制作IntelliJ折叠变量类型?

[英]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: 习惯于提供类型推断的语言(C ++,Scala),我发现这样的代码难以阅读:

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: 有没有办法让IntelliJ折叠变量的类型,以便我可以像这样读写它:

var classWriter = new ClassWriter(0);

but it actually stores it on disk as ClassWriter classWriter = new ClassWriter(0); 但它实际上将它作为ClassWriter classWriter = new ClassWriter(0);存储在磁盘上ClassWriter classWriter = new ClassWriter(0); ?

The Advanced Java Folding plugin (by JetBrains) adds folding for variable declarations , among other things. Advanced Java Folding插件(由JetBrains提供)为变量声明添加了折叠等功能。

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

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. 对于java,通常实例化具体的实现类型并通过抽象类型引用它。

For example: 例如:

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

In this circumstance, if you could just write var myList = new ArrayList<String>(); 在这种情况下,如果你可以写var myList = new ArrayList<String>(); - what type is myList actually? - myList实际上是什么类型的? Is it List<String> or ArrayList<String> ? List<String>还是ArrayList<String>

Strong typing is central to java and the example above illustrates why. 强类型是java的核心,上面的例子说明了原因。

Java 7 improves upon this somewhat, where the readability for parameterized types can use a diamond: Java 7对此有所改进,参数化类型的可读性可以使用菱形:

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

In any case, I would suggest to simply try and get used to the "java way" of doing this. 在任何情况下,我建议只是尝试并习惯这样做的“java方式”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM