Scala中的有效标识符字符 [英] Valid identifier characters in Scala

查看:121
本文介绍了Scala中的有效标识符字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到很困惑的一件事是知道我可以在方法和变量名中使用哪些字符和组合.例如

One thing I find quite confusing is knowing which characters and combinations I can use in method and variable names. For instance

val #^ = 1 // legal
val #  = 1 // illegal
val +  = 1 // legal
val &+ = 1 // legal
val &2 = 1 // illegal
val £2 = 1 // legal
val ¬  = 1 // legal

据我了解,字母数字标识符运算符标识符之间是有区别的.您可以将一个匹配项或另一个匹配项混合在一起,但不能同时混合两者,除非用下划线(混合标识符)分隔.

As I understand it, there is a distinction between alphanumeric identifiers and operator identifiers. You can mix an match one or the other but not both, unless separated by an underscore (a mixed identifier).

摘自 Scala编程第6.10节,

操作员标识符由一个或多个操作员字符组成. 运算符是可打印的ASCII字符,例如+,:,?,〜 或者 #.

An operator identifier consists of one or more operator characters. Operator characters are printable ASCII characters such as +, :, ?, ~ or #.

更准确地说,运算符属于Unicode集 数学符号(Sm)或其他符号(So)或7位 不是字母,数字,括号,正方形的ASCII字符 方括号,花括号,单引号或双引号或下划线, 句号,分号,逗号或反斜杠字符.

More precisely, an operator character belongs to the Unicode set of mathematical symbols(Sm) or other symbols(So), or to the 7-bit ASCII characters that are not letters, digits, parentheses, square brackets, curly braces, single or double quote, or an underscore, period, semi-colon, comma, or back tick character.

因此我们被禁止使用()[]{}'"_.;,和`

So we are excluded from using ()[]{}'"_.;, and `

我在 Wikipedia 上查找了Unicode数学符号,但发现的却不包括在内+:?等.在运算符字符的某个位置上是否有确凿的列表?

I looked up Unicode mathematical symbols on Wikipedia, but the ones I found didn't include +, :, ? etc. Is there a definitive list somewhere of what the operator characters are?

还有,为什么Unicode数学运算符(而不是符号)不算作运算符的任何想法?

Also, any ideas why Unicode mathematical operators (rather than symbols) do not count as operators?

推荐答案

使用规范中的EBNF语法:

Working from the EBNF syntax in the spec:

upper ::= ‘A’ | ... | ‘Z’ | ‘$’ | ‘_’ and Unicode category Lu
lower ::= ‘a’ | ... | ‘z’ and Unicode category Ll
letter ::= upper | lower and Unicode categories Lo, Lt, Nl
digit ::= ‘0’ | ... | ‘9’
opchar ::= "all other characters in \u0020-007F and Unicode
            categories Sm, So except parentheses ([]) and periods"

但还要考虑到词法语法的最开始定义:

But also taking into account the very beginning on Lexical Syntax that defines:

Parentheses ‘(’ | ‘)’ | ‘[’ | ‘]’ | ‘{’ | ‘}’.
Delimiter characters ‘‘’ | ‘’’ | ‘"’ | ‘.’ | ‘;’ | ‘,’

这就是我想出的.通过在\u0020-007F范围内进行消除,消除字母,数字,括号和定界符,我们可以使用opchar ...(鼓):

Here is what I come up with. Working by elimination in the range \u0020-007F, eliminating letters, digits, parentheses and delimiters, we have for opchar... (drumroll):

! # % & * + - / : < = > ? @ \ ^ | ~ 以及 Sm

! # % & * + - / : < = > ? @ \ ^ | ~ and also Sm and So - except for parentheses and periods.

(在此处添加有效的示例:).总而言之,下面是一些有效的示例,这些示例突出显示了所有情况-在REPL中注意\,我不得不转为\\:

( adding valid examples here:). In summary, here are some valid examples that highlights all cases - watch out for \ in the REPL, I had to escape as \\:

val !#%&*+-/:<=>?@\^|~ = 1 // all simple opchars
val simpleName = 1 
val withDigitsAndUnderscores_ab_12_ab12 = 1 
val wordEndingInOpChars_!#%&*+-/:<=>?@\^|~ = 1
val !^©® = 1 // opchars ans symbols
val abcαβγ_!^©® = 1 // mixing unicode letters and symbols


注1:

我发现了这个Unicode 类别索引以找出Lu, Ll, Lo, Lt, Nl :

I found this Unicode category index to figure out Lu, Ll, Lo, Lt, Nl:

  • Lu(大写字母)
  • Ll(小写字母)
  • Lo(其他字母)
  • Lt(标题栏)
  • Nl(字母数字,如罗马数字)
  • Sm(符号数学)
  • 所以(其他符号)

注2:

val #^ = 1 // legal   - two opchars
val #  = 1 // illegal - reserved word like class or => or @
val +  = 1 // legal   - opchar
val &+ = 1 // legal   - two opchars
val &2 = 1 // illegal - opchar and letter do not mix arbitrarily
val £2 = 1 // working - £ is part of Sc (Symbol currency) - undefined by spec
val ¬  = 1 // legal   - part of Sm

注3:

其他保留了单词的看似操作符的内容: _ : = => <- <: <% >: # @ 以及\u21D2⇒和\u2190

Other operator-looking things that are reserved words: _ : = => <- <: <% >: # @ and also \u21D2 ⇒ and \u2190

这篇关于Scala中的有效标识符字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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