Scala 常量的命名约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9745488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming convention for Scala constants?
提问by grautur
What is the naming convention for Scala constants? A brief search on StackOverflow suggestions uppercase CamelCase (the first line below), but I wanted to double-check.
Scala 常量的命名约定是什么?对 StackOverflow 建议大写 CamelCase 的简短搜索(下面的第一行),但我想仔细检查。
val ThisIsAConstant = 1.23
val THIS_IS_ANOTHER_CONSTANT = 1.55
val thisIsAThirdConstant = 1.94
Which is recommended Scala style?
哪个是推荐的 Scala 风格?
回答by Daniel C. Sobral
The officially recommended style (and I do mean officially) is the first style, camel case with first letter are upper case. It's laid down clearly by Odersky on Programming in Scala.
官方推荐的样式(我的意思是官方的)是第一种样式,带有第一个字母的驼峰式大小写是大写的。Odersky 在 Scala 编程中明确规定了这一点。
The style is also followed by the standard library, and has some support in language semantics: identifiers starting with upper case are treated as constants in pattern matching.
该样式也遵循标准库,并且在语言语义上有一些支持:以大写开头的标识符在模式匹配中被视为常量。
(Section 6.10, p. 107 in the second edition)
(第 6.10 节,第二版第 107 页)
回答by Leif Wickland
(This is an addendum comment to Daniel's answer, but I'm posting it as an answer for the benefit of syntax highlighting and formatting.)
(这是对 Daniel 答案的补充评论,但为了语法突出显示和格式设置,我将其发布为答案。)
Daniel's point about the style of using an initial capital letter being important in the language semantics is more subtle and important than I originally gave it credit for when I learned Scala.
Daniel 关于使用首字母大写的风格在语言语义中很重要的观点比我在学习 Scala 时最初认为的更加微妙和重要。
Consider the following code:
考虑以下代码:
object Case {
val lowerConst = "lower"
val UpperConst = "UPPER"
def main(args: Array[String]) {
for (i <- Seq(lowerConst, UpperConst, "should mismatch.").map(Option.apply)) {
print("Input '%s' results in: ".format(i))
i match {
case Some(UpperConst) => println("UPPER!!!")
case Some(lowerConst) => println("lower!")
case _ => println("mismatch!")
}
}
}
}
Naively I would have expected that to reach all of the cases in the match. Instead it prints:
我天真地以为这会影响比赛中的所有情况。相反,它打印:
Input 'Some(lower)' results in: lower!
Input 'Some(UPPER)' results in: UPPER!!!
Input 'Some(should mismatch.)' results in: lower!
What's going on is that the case Some(lowerConst)shadows the val lowerConstand creates a local variable of the same name which will be populated any time a Somecontaining a string is evaluated.
发生的事情是case Some(lowerConst)阴影 vallowerConst并创建一个同名的局部变量,该变量将在Some包含字符串的任何时候被填充。
There are admittedly ways to work around it, but the simplest is to follow the style guide for constant naming.
无可否认,有一些方法可以解决它,但最简单的方法是遵循常量命名的样式指南。
If you can't follow the naming convention, then as @reggoodwin points out in the comments below, you can put the variable name in ticks, like so
如果您不能遵循命名约定,那么正如@reggoodwin 在下面的评论中指出的那样,您可以将变量名称放在勾号中,就像这样
case Some(`lowerConst`) => println("lower!")
回答by samthebest
Constant names should be in upper camel case. That is, if the member is final, immutable and it belongs to a package object or an object, it may be considered a constant .... Method, Value and variable names should be in lower camel case
http://docs.scala-lang.org/style/naming-conventions.html#constants-values-variable-and-methods
常量名应该是大写的驼峰式。也就是说,如果成员是 final 的,不可变的,并且它属于包 object 或 object,它可以被认为是一个常量....方法,值和变量名称应该是小驼峰
http://docs.scala-lang.org/style/naming-conventions.html#constants-values-variable-and-methods

