Why is a single underscore character an illegal name for a lambda parameter?

Deadly 提交于 2019-12-21 03:17:06

问题


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:

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?


回答1:


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

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.)



来源:https://stackoverflow.com/questions/34521690/why-is-a-single-underscore-character-an-illegal-name-for-a-lambda-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!