scala - Scala 定义的命名约定
问题描述
我试图在 scala 中定义名称 +I,如下所示。
def +I(i1: Int, i2: Int): Int = { .....}
它说“=”预期但发现标识符。如果我做 I_+,它可以工作,但为了便于使用,我想要 +I。难道不能用这个名字来定义吗?
解决方案
可以使用这样的名称进行定义,但看起来不太好。您需要将名称放在反引号之间:
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
推荐阅读
- python-3.x - Python For循环花费太多时间
- visual-studio-code - 为什么 VS Code 使用自定义 AMD 加载程序而不是流行的捆绑程序?
- jpa - Axon Saga 中执行信息的持久性
- c++ - 未知测试用例因快速排序而失败
- react-native - ImagePicker EXPO 长宽比
- flutter - 在 Flutter 中用颜色填充图标
- nltk - 将 tf-idf 输出运行到 nltk.model
- c# - I am trying to check that If email already exist in databse or not or not in database but my code throws error
- flutter - 当我在 FutureBuilder 中调用 ListView.builder 时,总是崩溃
- google-sheets - 谷歌表格 - 计算有多少行符合这些条件