为什么变量名中不允许使用特殊字符? [英] Why are special characters not allowed in variable names?

查看:114
本文介绍了为什么变量名中不允许使用特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么编程语言的变量名中不允许使用特殊字符(下划线除外)?是否有任何与计算机架构或组织相关的原因.

Why special character( except underscore) are not allowed in variable name of programming language ? Is there are any reason related to computer architecture or organisation.

推荐答案

大多数语言都有很长的历史,使用 ASCII 字符集.这些语言往往有简单的标识符描述(例如,以 A-Z 开头,然后是 A-Z,0-9,可能是下划线;COBOL 允许-"作为名称的一部分).当你只有一个 029 按键或电传打字机时,你没有很多其他字符,而且大多数都被用作运算符语法或标点符号.

Most languages have long histories, using ASCII character sets. Those languages tend to have simple identifier descriptions (e.g., starts with A-Z, followed by A-Z,0-9, maybe underscore; COBOL allows "-" as part of a name). When all you had was an 029 keypunch or a teletype, you didn't have many other characters, and most of them got used as operator syntax or punctuation.

在较旧的机器上,这确实具有优势,您可以将标识符编码为基数 37 (AZ,0-9, null) [32 位中的 6 个字符] 或基数 64 (AZ,az,0-9,下划线和空)数字(36 位中的 6 个字符,早期机器中的常见字长)用于小符号表.结果是:许多旧语言对标识符大小有 6 个字符的限制(例如 FORTRAN).

On older machines, this did have the advantage that you could encode an identifier as a radix 37 (A-Z,0-9, null) [6 characters in 32 bits] or radix 64 (A-Z,a-z,0-9,underscore and null) numbers [6 characters in 36 bits, a common word size in earlier generations of machines) for small symbol tables. A consequence: many older languages had 6 character limits on identifier sizes (e.g., FORTRAN).

LISP 语言一直以来都更加宽容;名称可以是对 LISP 具有特殊意义的字符以外的任何字符,例如 ( ) [ ] ' ` #,并且通常有一些方法可以使用某种转义约定将这些字符插入到名称中.我们的 PARLANSE 语言就像 LISP;它使用~"作为转义符,因此您可以将 ~(begin+~)end 写为单个标识符,其实际拼写为(begin+end)".

LISP languages have long been much more permissive; names can be anything but characters with special meaning to LISP, e.g., ( ) [ ] ' ` #, and usually there are ways to insert these characters into names using some kind of escape convention. Our PARLANSE language is like LISP; it uses "~" as an escape, so you can write ~(begin+~)end as a single identifier whose actual spelling is "(begin+end)".

更现代的语言(Java、C#、Scala、....、呃,甚至 PARLANSE)在 Unicode 时代成长起来,并且倾向于允许标识符中的大部分 unicode(实际上,它们倾向于允许命名的 Unicode 子集作为标识符的一部分).由汉字构成的标识符在此类语言中是完全合法的.

More modern languages (Java, C#, Scala, ...., uh, even PARLANSE) grew up in an era of Unicode, and tend to allow most of unicode in identifiers (actually, they tend to allow named Unicode subsets as parts of identifiers). An identifier made of chinese characters is perfectly legal in such languages.

这在西半球是一种品味问题:大多数标识符名称仍然倾向于仅使用字母和数字(有时,西欧字母).我不知道日语和中文在具有支持 Unicode 的字符集时真正使用什么作为标识符名称;我所看到的亚洲代码倾向于遵循西方标识符约定,但注释倾向于使用更多的 Unicode 字符集.

Its kind of a matter of taste in the Western hemisphere: most identifier names still tend to use just letters and digits (sometimes, Western European letters). I don't know what the Japanese and Chinese really use for identifier names when they have Unicode capable character sets; what little Asian code I have seen tends to follow western identifier conventions but the comments tend to use much more of the Unicode character set.

这篇关于为什么变量名中不允许使用特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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