本文介绍了为什么单个下划线字符是lambda参数的非法名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试命名一个lambda参数 _ ,例如(缩减版):

I tried naming a lambda parameter _, e.g. (a cut down version):

Consumer<Object> c = _ -> {};

因为我想表示参数被忽略,但我得到以下编译错误:

as I wanted to signify that a parameter was being ignored, but I got the following compiler error:

这对我来说是一个惊喜。有趣的是,两个下划线是可以的:

This was a surprise for me. Interestingly, two underscores is OK:

Consumer<Object> c = __ -> {}; // no compile error

所以它一般不是下划线字符,而是单个字符。

So it's not the underscore character in general, but a single one.

为什么单下划线名称是专门禁止的?

Why is the single-underscore name specifically forbidden?

推荐答案

原因用 https://stackoverflow.com/users/3553087/brian-goetz?tab=profile\"> Brian Goetz 本人:

The reason is expressed in this post from Brian Goetz himself:

这篇关于为什么单个下划线字符是lambda参数的非法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:00