Naming convention for Scala constants?

ScalaNaming ConventionsConstants

Scala Problem Overview


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.

val ThisIsAConstant = 1.23
val THIS_IS_ANOTHER_CONSTANT = 1.55
val thisIsAThirdConstant = 1.94

Which is recommended Scala style?

Scala Solutions


Solution 1 - Scala

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.

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)

Solution 2 - Scala

(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'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.

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 lowerConst and creates a local variable of the same name which will be populated any time a Some containing a string is evaluated.

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

case Some(`lowerConst`) => println("lower!")

Solution 3 - Scala

> 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongrauturView Question on Stackoverflow
Solution 1 - ScalaDaniel C. SobralView Answer on Stackoverflow
Solution 2 - ScalaLeif WicklandView Answer on Stackoverflow
Solution 3 - ScalasamthebestView Answer on Stackoverflow