E005: Duplicate Bind

This error is emitted when the same variable name is used more than once in a pattern match case. Each bound variable in a case pattern must have a unique name.

For each case bound variable names have to be unique. In:

case (a, a) => a

a is not unique. Rename one of the bound variables!


Example

def test(x: Any) = x match
  case (a, a) => a

Error

-- [E005] Naming Error: example.scala:2:11 -------------------------------------
2 |  case (a, a) => a
  |           ^
  |           duplicate pattern variable: a
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | For each case bound variable names have to be unique. In:
  |
  | case (a, a) =>  {
  |   a
  | }
  |
  | a is not unique. Rename one of the bound variables!
   -----------------------------------------------------------------------------

Solution

// Use unique names for each bound variable
def test(x: Any) = x match
  case (a, b) => (a, b)
// Use wildcard _ if you don't need the value
def test(x: Any) = x match
  case (a, _) => a
// Use a guard if you want to match equal values
def test(x: Any) = x match
  case (a, b) if a == b => a