Valid identifier characters in Scala

后端 未结 2 1761
太阳男子
太阳男子 2020-11-28 04:28

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 #          


        
2条回答
  •  甜味超标
    2020-11-28 04:42

    The language specification. gives the rule in Chapter 1, lexical syntax (on page 3):

    1. Operator characters. These consist of all printable ASCII characters \u0020-\u007F. which are in none of the sets above, mathematical sym- bols(Sm) and other symbols(So).

    This is basically the same as your extract of Programming in Programming in Scala. + is not an Unicode mathematical symbol, but it is definitely an ASCII printable character not listed above (not a letter, including _ or $, a digit, a paranthesis, a delimiter).

    In your list:

    1. # is illegal not because the character is not an operator character (#^ is legal), but because it is a reserved word (on page 4), for type projection.
    2. &2 is illegal because you mix an operator character & and a non-operator character, digit 2
    3. £2 is legal because £ is not an operator character: it is not a seven bit ASCII, but 8 bit extended ASCII. It is not nice, as $ is not one either (it is considered a letter).

提交回复
热议问题