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?!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With