为什么try-with-resource需要局部变量? [英] Why does try-with-resource require a local variable?

查看:108
本文介绍了为什么try-with-resource需要局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考我的问题我想知道为什么try-with-resource-statement根本需要一个 named 局部变量. java.util.concurrent.locks.Lock的AutoCloseable包装器有任何风险吗?/p>

我当前的用法如下:

  try(AutoCloseableReentrantReadWiteLock.Lock l = _lock.writeLock()){//做一点事} 

变量 l 在try块内未使用,仅污染名称空间.据我所知,类似的C# using 语句不需要本地命名变量.

是否有任何原因不支持以下情况,并且在try块末尾关闭了一个匿名局部变量?

  try(_lock.writeLock()){//做一点事} 

解决方案

@McDowell评论中的链接显示了引入了try-with-resources语句的Java技术规范:

在JDK 7中,我们从try-with-resources构造开始允许将通用表达式用于资源,包括方法调用.但是,专家组通过早期发现审查草案( http://jcp.org/aboutJava/communityprocess/edr/jsr334/index.html ),

"[对try-with-resources statemenbt的未来可能的更改是放弃对将被指定为常规资源的支持表达.非同寻常的规范和实现复杂性由于允许将通用表达式用作资源而产生.一种受限制的表达式,它可以是标识符或PrimaryNoNewArray可能就足够了.甚至更严格的限制仅仅允许一个标识符就可以提供几乎所有的附加允许使用完整表达式(在强制声明时)的实用程序一个新的资源变量)以更低的边际实施量实现;规范影响."

到JDK 7结束时,我们想要的是一个新的变量声明资源或现有的最终/有效最终变量.我们只有时间在7中提供前者;在9中,我们提供了后者也是如此.

With reference to my question Any risk in a AutoCloseable wrapper for java.util.concurrent.locks.Lock?, I am wondering why the try-with-resource-statement require a named local variable at all.

My current usage is as follows:

try (AutoCloseableReentrantReadWiteLock.Lock l = _lock.writeLock()) {
    // do something
}        

The variable l is unused inside the try block and only pollutes the namespace. From what I can remember the analogous C# using-statement does not require a local named variable.

Is there any reason the following could not have been supported, with an anonymous local variable that is closed at the end of try block?

try (_lock.writeLock()) {
    // do something
}        

解决方案

The link in the comment by @McDowell reveals the correct answer in a blog post comment by Joe Darcy who led the Java Technology Specification that introduced the try-with-resources statement:

Back in JDK 7, we started with a try-with-resources construct like that allowed a general expression to be used for the resource, including a method call. However, the expert group found by the early draft review (http://jcp.org/aboutJava/communityprocess/edr/jsr334/index.html) that

"A possible future change [to the try-with-resources statemenbt] is dropping support for a resource to be specified as a general Expression. Nontrivial specification and implementation complexities arise from allowing a general Expression to be used as resource. A restricted expression that could be an identifier or a PrimaryNoNewArray may suffice. Even the more severe restriction of just allowing an identifier may provide nearly all the additional utility of allowing a full expression (over forcing the declaration of a new resource variable) at a much lower marginal implementation and specification impact."

By the end of JDK 7, what we wanted was a fresh variable declaration for the resource or an existing final / effectively final variable. We only had time to provide the former in 7; in 9, we are providing the latter too.

这篇关于为什么try-with-resource需要局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆