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

查看:30
本文介绍了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 `

我在 维基百科上查找了 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... (drumroll):

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):

<代码>!#% &* + -/: <=>?@^|~还有 SmSo - 括号和句号除外.

! # % & * + - / : < = > ? @ ^ | ~ 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

<小时>

注一:

我找到了这个 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天全站免登陆