LowercasePatternMatchChecker justification

38 views
Skip to first unread message

richar...@redmart.com

unread,
Dec 11, 2016, 11:21:47 PM12/11/16
to Scalastyle users
Hello,

I have a question about the justification behind the LowercasePatternMatchChecker in more complex scenarios. The documentation (http://www.scalastyle.org/rules-dev.html#org_scalastyle_scalariform_LowercasePatternMatchChecker) explains that "A lower case pattern match clause with no other tokens is the same as _". However, this is not the case with multiple tokens nor with conditional case statements, but the rule is still applied.

Given the example:

"foo" match {
  case bar if bar.isEmpty => bar
  case _ => "empty"
}

Scalastyle will complain that the case statement identifier bar is lowercase and should therefore be either:
  1. uppercased (which violates http://docs.scala-lang.org/style/naming-conventions.htmlvariable names should be in lower camel case),
  2. replaced with _ (which cannot be done due to the conditional comparison),
  3. explicitly typed as Any (which causes the conditional comparison to fail at compile time)
Therefore I think that the documentation should be updated to explicitly state that uppercase should only be used for stable identifiers that are constants and that the type used to strongly type the variable with should be the type of the input value to avoid breaking conditionals.

In addition, my understanding of the issue around lowercase pattern matches purely revolves around avoiding suspicious shadowing of stable identifiers in the outer scope, which is recommended to be avoided by either using backticks for variables or uppercase for constants - see http://www.scala-lang.org/files/archive/spec/2.11/08-pattern-matching.html. Therefore should this checker just check that the variable name doesn't shadow an outer scope variable and allow untyped lowercase variables? Or is there another source of potential confusion that I am missing?
Reply all
Reply to author
Forward
0 new messages