0

I've been looking at someone's code and noticed this

case overdue @ PendingOperation(operation, _) => ......

What does @ symbol mean here? How is it called?

kikulikov
  • 2,512
  • 4
  • 29
  • 45

1 Answers1

3

It's called variable binding. It binds the value being matched on to the overdue variable.

In this way you can both destructure the value by pattern matching over it and later reference it as a whole.

While it's not particularly useful in a case like

foo match {
  case a @ Bar(baz, bar) => ...
}

since you can directly refer to foo, it gets more interesting in cases like

foo match {
  case Bar(b @ Baz("hello", _), _) => ...
}

where you bind an inner match to a variable.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235