(This is a commentary on the addition to Daniel, but I am posting it as an answer in favor of syntax highlighting and formatting.)
Daniel believes that the style of using the initial capital letter, which is important in the semantics of the language, is more subtle and important than I initially gave him credit 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 expect that he will reach all 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 happens is that case Some(lowerConst) obscures val lowerConst and creates a local variable with the same name that will be populated anytime the Some string containing the string is evaluated.
True, there are ways around it, but the easiest way is to follow the style guide for permanent naming.
If you cannot follow the naming convention, then, as @reggoodwin points out in the comments below, you can put the variable name in ticks, for example
case Some(`lowerConst`) => println("lower!")
Leif Wickland Mar 17 '12 at 18:25 2012-03-17 18:25
source share