为什么必须用lambda体中的完整代码块来包含throw语句? [英] Why must throw statements be enclosed with a full code block in a lambda body?

查看:323
本文介绍了为什么必须用lambda体中的完整代码块来包含throw语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果lambda函数中有一个语句,我们可以省略为它定义完整的代码块:

If there is a single statement in a lambda function, we can omit defining the full code block for it:

new Thread(() -> System.out.println());

为什么抛出异常的语句不是这种情况?这会产生一个编译错误,指出'{'期望

Why is that not the case for statements that throw exceptions? This yields a compilation error stating '{' expected:

new Thread(() -> throw new RuntimeException());

当然,将lambda主体包含在代码块中可以工作:

Of course, enclosing the lambda body in a code block works:

new Thread(() -> {
    throw new RuntimeException();
});


推荐答案

A 抛出语句是一个语句,而不是一个表达式,所以它必须放在大括号内。根据这篇文章,Java专家组对此进行了非正式的调查。 lambdas当时的语法,有四个选项:

A throw statement is, well, a statement and not an expression so it must be placed within braces. According to this article, the Java Expert Group had an informal survey on the syntax of lambdas at the time, and there were four options:


  • Strawman #(arglist)(expr)和#(arglist){statements}

  • BGGA {args - >语句} (类似于Scala和Groovy)

  • SotL #{args - >声明}

  • Redmond (args) - > {statements}

  • Strawman: #(arglist)(expr) and #(arglist){statements}
  • BGGA: { args -> statements } (similar to Scala and Groovy)
  • SotL: #{ args -> statements}
  • Redmond: (args) -> { statements }

最终,选择采用类似于C#的语法,根据< a href =http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html\"rel =noreferrer>这个帖子,也看起来最接近最后一个据我所知,上面的选项。在C#中,表达式lambdas 语句lambdas 之间存在区别:

Ultimately, the choice was to adopt a syntax similar to that of C# according to this thread, which also looks closest to the last option above as far as I can see. In C#, there is a distinction between expression lambdas and statement lambdas:

表达式lambda(C# ):

(input parameters) => expression

语句lambda(C#):

(input parameters) => {statement;}  

语法在此MSDN文档页面

选择此语法的基本原理在上一个帖子中提到了其他选项:

And the rationale for choosing this syntax over the other options is mentioned in the previous thread:


选择此语法的决定是双重的:

The decision to choose this syntax was twofold:


  • 语法在大多数主观测量中得分相当不错(尽管
    的情况看起来很糟糕,就像所有其他人一样)。特别是在
    中,它适用于作为方法
    参数(常见情况)的小lambdas,并且也适用于大
    (多语句)lambdas。

  • The syntax scores "pretty well" on most subjective measures (though has cases where it looks bad, just like all the others do). In particular, it does well with "small" lambdas that are used as method arguments (a common case), and also does well with large (multi-statement) lambdas.

尽管进行了广泛的搜索,但是在
替代品中没有明显的赢家(每种形式都有一些好的方面,有些确实不是很好的b $ b好​​方面并且没有任何形式明显优于其他的
)。因此,我们觉得最好选择一些已经证明可以在两种语言中运行良好的b $ b的东西,这些语言最像是
Java - C#和Scala - 而不是发明新东西。

Despite extensive searching, there was no clear winner among the alternatives (each form had some good aspects and some really not very good aspects, and there was no form that was clearly better than the others). So, we felt that it was better to choose something that has already been shown to work well in the two languages that are most like Java -- C# and Scala -- rather than to invent something new.

这篇关于为什么必须用lambda体中的完整代码块来包含throw语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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