Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In scala pattern matching, what is suspicious shadowing by a variable pattern?

When I type the following code into Intellij, it highlights the x inside the match with the warning "Suspicious shadowing by a Variable Pattern"

val x = "some value" "test" match {   case x => } 

It suggests that I change it to:

val x = "some value" "test" match {   case `x` => //note backticks } 

What is suspicious shadowing and what do the backticks do?!

like image 285
Noel Kennedy Avatar asked Oct 26 '11 15:10

Noel Kennedy


1 Answers

case x 

creates a variable named x, which would match everything and since a variable with the same name already exists you shadow it by using the same name.

case `x` 

uses the value of the variable x which was declared before and would only match inputs with same values.

PS

You can leave the back ticks out if the name of the variable is capitalized as in

case Pi 

Watch Pattern Matching Unleashed for more.

like image 172
agilesteel Avatar answered Sep 28 '22 08:09

agilesteel