为什么单个下划线字符是lambda参数的非法名称? [英] Why is a single underscore character an illegal name for a lambda parameter?

查看:170
本文介绍了为什么单个下划线字符是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:


对于lambda参数禁止使用'_'作为标识符

use of '_' as an identifier is forbidden for lambda parameters

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

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:


我们从标识符空间回收_的句法空间,用于未来的语言特征。但是,因为存在可能使用它的现有程序,所以它是对现有语法位置8中出现的标识符的警告,以及lambda形式的错误(因为没有现有的lambdas代码。)

We are "reclaiming" the syntactic real estate of "_" from the space of identifiers for use in future language features. However, because there are existing programs that might use it, it is a warning for identifiers that occur in existing syntactic positions for 8, and an error for lambda formals (since there is no existing code with lambdas.)

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

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