“受限制的关键字”与“受限制的关键字”之间的概念差异是什么?和“保留类型名称”在Java 10? [英] What is the conceptual difference between a "restricted keyword" and "reserved type name" in Java 10?

查看:106
本文介绍了“受限制的关键字”与“受限制的关键字”之间的概念差异是什么?和“保留类型名称”在Java 10?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 10附带了新的Local Variable Type Inference。令牌 var 可用于减少声明变量时所需的样板。例如。

Java 10 comes with the new Local Variable Type Inference. The token var can be used to reduce the boilerplate required when declaring a variable. e.g.

var s = "hello";

根据什么类型的令牌正好是var在Java 10?中,这个新标记不是关键字,而是保留类型名称。因此,单词var仍然可以用作变量名,它保持与现有代码的向后兼容性。

According to What type of token is exactly "var" in Java 10? this new token is not a "keyword" but rather is a "reserved type name". As such the word "var" can still be used as a variable name which maintains backwards compatibility with existing code.

var var = "you can do this";

当在Java 9中引入模块功能时,这个新令牌的类型(及其其他9个相关的令牌被称为受限制的关键词。也就是说,在某些特定于上下文的限制下,它们仅被视为关键字。例如您仍然可以使用名为module的变量。

When the "module" feature was introduced in Java 9 the type of this new token (along with its 9 other related tokens) were called a "restricted keywords". Which is to say they were only considered keywords under certain context specific restrictions. e.g. you can still have variables called module.

当新的语言功能以不会破坏现有用户定义符号的方式添加到C ++时,它们被称为上下文 - 敏感关键字。

When new language features were added to C++ in such a way that they did not clobber existing user defined symbols they were called "context-sensitive keywords".

Java 10中新的保留类型名称 var 标记之间是否存在概念差异和上下文敏感的关键字或受限制的关键字。也就是说,在某些特定于上下文的限制下,新的 var 令牌实际上只是一个关键字。如果是这种情况,为什么不将它简单地添加到受限制的关键字列表中?

Is there a conceptual difference between the new "reserved type name" var token in Java 10 and a "context-sensitive keyword" or "restricted keyword". Which is to say isn't the new var token really just a keyword under certain context specific restrictions. If that is the case why wasn't it simply added to the list of "restricted keywords"?

为了进一步增加我的混淆 JLS 说:

To add to my confusion further the current draft version of the JLS says that:


字符序列var通常被视为标识符,但在某些特殊情况下,
就像是关键字一样。

The character sequence var is generally treated as an identifier, but in certain special cases acts as if it were a keyword instead.

这个定义听起来像是一个受限制的关键字。

That definition certainly sounds like a "restricted keyword".

推荐答案

您引用的部分的下一句话(3.8:关键字)是:

The very next sentence of the section you quote (3.8: Keywords) is:


另外十个字符序列是受限制的关键字:open,module,requires,transitive,exports,opens,to,uses,provide和with。

A further ten character sequences are restricted keywords: open, module, requires, transitive, exports, opens, to, uses, provides, and with.

请注意 var 不在此列表中。本节中提到的 var 正是为了清楚地表明,虽然在某些情况下它可能行为就像一个受限制的关键字,而它在你对规范的非正式阅读中可能听起来那样,但事实并非如此。

Note that var is not on this list. The mention of var in this section was included precisely to make it clear that while in some cases it may act like a restricted keyword, and while it may sound that way to you in your informal reading of the specification, it is not.

上下文相关关键字是我们可以使用的工具之一,用于以兼容的方式演变语言;保留标识符是另一个。在这种特殊情况下,可以应用任何一种,并且最后,考虑后一种工具(出于规范和编译器实现的目的)是优选的。

Context-sensitive keywords is one of the tools we have at our disposal for evolving the language in a compatible way; reserved identifiers are another. In this particular case, either could have been applied, and in the end, the latter tool was considered (for the purposes of specification and compiler implementation) to be preferable.

与大多数编译器实现一样,规范将词法,句法和输入问题分开。关键字主要在词法分析器和解析器产生的层次上处理;保留类型名称将在编译过程的后期检查,在类型分析期间,并且可以与非保留名称共享解析器产品。

The specification, like most compiler implementations, separates lexical, syntactic, and typing concerns. Keywords are handled primarily at the level of lexer and parser productions; reserved type names are checked later in the compilation process, during type analysis, and can share the parser productions with non-reserved names.

从开发人员的角度来看无论是规范作者还是编译器实现者,差异主要是理论上的;任何一种方式都可以达到预期的效果。

From the perspective of a developer who is neither a spec author nor a compiler implementor, the difference is largely theoretical; the desired effect can be accomplished with either path.

这篇关于“受限制的关键字”与“受限制的关键字”之间的概念差异是什么?和“保留类型名称”在Java 10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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