我正在尝试在scala中定义名称+ I,如下所示。
def +I(i1: Int, i2: Int): Int = { .....}
它说' ='预期但找到了标识符。如果我做I_ +,它可以工作,但为了方便使用,我想要+我。是否不能为定义命名?
答案 0 :(得分:5)
可以将这样的名称用于定义,但它看起来不太好。你需要在反引号之间加上名字:
def `+I`(i1: Int, i2: Int): Int = 42
用法:
`+I`(1, 2)
Scala的词法语法在https://www.scala-lang.org/files/archive/spec/2.11/13-syntax-summary.html
中进行了总结以下是最相关的详细信息:
whiteSpace ::= ‘\u0020’ | ‘\u0009’ | ‘\u000D’ | ‘\u000A’
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’
paren ::= ‘(’ | ‘)’ | ‘[’ | ‘]’ | ‘{’ | ‘}’
delim ::= ‘`’ | ‘'’ | ‘"’ | ‘.’ | ‘;’ | ‘,’
opchar ::= // printableChar not matched by (whiteSpace | upper | lower |
// letter | digit | paren | delim | opchar | Unicode_Sm | Unicode_So)
printableChar ::= // all characters in [\u0020, \u007F] inclusive
charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘)
op ::= opchar {opchar}
varid ::= lower idrest
plainid ::= upper idrest
| varid
| op
id ::= plainid
| ‘`’ stringLiteral ‘`’
idrest ::= {letter | digit} [‘_’ op]
FunDcl ::= FunSig [‘:’ Type]
FunSig ::= id [FunTypeParamClause] ParamClauses