This match statement can be rewritten as an anonymous pattern-matching function
46 var substr = lang2.substring(0, lang2.length - 1)
47 }
48
49 lang1.foreach { c =>50 c match {
51 case 'a' => println('a')
52 case 'b' => println('b')
Description
Use anonymous function when pattern matching element(s) rather than explicitly using the match
keyword when possible. This approach/syntax is cleaner, improves readability and is easy to comprehend.
Bad practice
list.foreach {element =>
element match {
case foo =>
case bar =>
case _ =>
}
}
Recommended
list.foreach {
case foo =>
case bar =>
case _ =>
}