问题
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";
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";
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.
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".
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"?
To add to my confusion further the current draft version of the JLS says that:
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".
回答1:
The very next sentence of the section you quote (3.8: Keywords) is:
A further ten character sequences are restricted keywords: open, module, requires, transitive, exports, opens, to, uses, provides, and with.
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.
来源:https://stackoverflow.com/questions/49102553/what-is-the-conceptual-difference-between-a-restricted-keyword-and-reserved-t