为什么变量名中不能包含空格? [英] Why can't variable names have spaces in them?

查看:630
本文介绍了为什么变量名中不能包含空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:为什么变量名不能以数字开头?

变量名中不允许使用空格还是出于惯例?

Is there a technical reason why spaces aren't allowed in variable names or is it down to convention?

例如,是什么阻止我们做这样的事情?:

For example, what's stopping us from doing something like this?:

average score = sum of scores / number of scores

想到的唯一一个问题是关键字,但是可以简单地将它们限制在变量名中使用,而词法分析器将能够区分变量的一部分和关键字.

The only issue that comes to mind is keywords, but one could simply restrict the use of them in a variable name, and the lexer would be able to distinguish between part of a variable and a keyword.

推荐答案

除了语言设计者的决定和单令牌标识符的历史之外,没有根本的原因.实际上,某些语言确实允许使用多令牌标识符:MultiMedia Fusion的表达语言,一些Mac电子表格/笔记本软件,其名称使我不知所措,我敢肯定还有其他语言.尽管如此,有几个考虑因素使这个问题变得微不足道.

There’s no fundamental reason, apart from the decisions of language designers and a history of single-token identifiers. Some languages in fact do allow multi-token identifiers: MultiMedia Fusion’s expression language, some Mac spreadsheet/notebook software whose name escapes me, and I’m sure of others. There are several considerations that make the problem nontrivial, though.

假设语言是自由格式,则需要规范的表示形式,以便像account name这样的标识符被视为相同,而与空白无关.编译器可能需要使用一些处理约定来取悦链接器.然后,您必须考虑它对外贸出口的影响-为什么C ++具有extern "C"链接说明符来禁用重整.

Presuming the language is free-form, you need a canonical representation, so that an identifier like account name is treated the same regardless of whitespace. A compiler would probably need to use some mangling convention to please a linker. Then you have to consider the effect of that on foreign exports—why C++ has the extern "C" linkage specifier to disable mangling.

正如您所见,关键字是一个问题.大多数C系列语言都具有与标识符不同的词法类关键字,这些标识符与上下文无关.您不能在C ++中命名变量class.这可以通过禁止多令牌标识符中的关键字来解决:

Keywords are an issue, as you have seen. Most C-family languages have a lexical class of keywords distinct from identifiers, which are not context-sensitive. You cannot name a variable class in C++. This can be solved by disallowing keywords in multi-token identifiers:

if account age < 13 then child account = true;

在这里,ifthen不能是标识符的一部分,因此与account agechild account毫无歧义.或者,您可以在任何地方都要求标点符号:

Here, if and then cannot be part of an identifier, so there is no ambiguity with account age and child account. Alternatively, you can require punctuation everywhere:

if (account age < 13) {
  child account = true;
}

最后一个选择是使关键字普遍与上下文相关,从而导致诸如以下的怪异现象:

The last option is to make keywords pervasively context-sensitive, leading to such monstrosities as:

IF IF = THEN THEN ELSE = THEN ELSE THEN = ELSE

最大的问题是,并置是一种非常强大的句法结构,您不想轻易占据它.允许多令牌标识符可防止将并置用于其他目的,例如功能应用程序或组合.我认为,最好允许大多数 nonwhitespace 字符,从而允许使用canonical-venomous-frobnicator这样的标识符.仍然可读性强,但产生歧义的机会较少.

The biggest issue is that juxtaposition is an extremely powerful syntactic construct, and you don’t want to occupy it lightly. Allowing multi-token identifiers prevents using juxtaposition for another purpose, such as function application or composition. Far better, I think, just to allow most nonwhitespace characters and thereby permit such identifiers as canonical-venomous-frobnicator. Still plenty readable but with fewer opportunities for ambiguity.

这篇关于为什么变量名中不能包含空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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