您的位置:首页 > 编程语言 > Java开发

JDK将新增局部变量类型推断功能

2016-03-18 00:14 330 查看
OpenJDK 提出一个新的计划, 准备为 Java 增加局部变量类型推断功能(local-variable type inference capability), 这让代码编写更简单, 同时也保证原有的静态类型安全特性。

该计划于3月8号提出,文档地址为: JEP-286(JDK Enhancement Proposal,JDK增强方案)。 目前没有指定具体在哪个JDK版本中实现。

提案中说:

“We seek to improve the developer experience by reducing the ceremony associated with writing Java code while maintaining Java’s commitment to static type safety by allowing developers to elide the often-unnecessary manifest declaration of local variable types, (我们致力于改善编码体验, 减少非必要的代码量,同时保持Java静态类型安全的承诺,允许开发者省略不必要的类型声明).”

其他静态类型语言, 如 C++, Scala, 和 Go,都已经实现了局部变量类型推断。

JEP 中强调:

“Java is nearly the only popular statically typed language that has not embraced local-variable type inference,( Java应该是最流行的几种静态类型语言中, 唯一不支持局部变量类型推断的). ”

“At this point, this should no longer be a controversial feature.(在这一点上,应该不会有太多争议).”

Java开发人员经常会发现标准样板代码体积臃肿。

“Manifest type declarations for locals are often perceived to be unnecessary or even in the way; given good variable naming, it is often perfectly clear what’s going on, (本地变量的类型清单通常来说是非必须的,指定一个有意义的变量名比什么都好).”

JEP中写到。

“The need to provide a manifest type for every variable also accidentally encourages developers toward overly complex expressions; with a lower-ceremony declaration syntax, there is less disincentive to break complex chained or nested expressions into simpler ones.(为每个变量指定类型也可能会导致写出很复杂的语句, 简化语法声明, 可以有效减少复杂的链式调用, 将嵌套表达式分解为简单语句。)”

该计划限制了类型推断的使用场景: 有初始值的局部变量, 增强型for循环中的index, 普通for循环里的局部变量

“Quantitatively, we want that a substantial percentage of local variable declarations in real code bases can be converted using this feature, inferring an appropriate type.(在数量上, 我们希望可以根据这个特性,将绝大部分的局部变量, 推导出一个适当的类型声明。)”

OpenJDK作为Java SE 的开源实现, 由Oracle提供支持。JavaSE 9 是Oracle官方的下一代版本,即将在一年以内发布。JEP-286由Oracle的 Java平台组(Java platform group)首席架构师 Mark Reinhold 背书。

欢迎加入: CNC开源组件交流群 316630025

原文链接: http://www.javaworld.com/article/3044554/java-language/openjdk-plan-would-make-java-coding-easier.html

原文日期: 2016年03月15日

翻译日期: 2016年03月17日

翻译人员: 铁锚 http://blog.csdn.net/renfufei
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: