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

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.

Leave a Comment