0

When studying sources of akka I found the following in the akka.event.slf4j.SLF4JLogger actor:

def receive = {
    //...
    case event @ Warning(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).warn("{}", message.asInstanceOf[AnyRef]) }

    case event @ Info(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).info("{}", message.asInstanceOf[AnyRef]) }

    case event @ Debug(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).debug("{}", message.asInstanceOf[AnyRef]) }
    //...
}

I didn't quite understand what is the @ sign. It was not a method and there's no declaration of the event in scope. Warning, Info and Debug are all objects with apply methods.

1 Answers1

2

It's called variable binding:

In addition to the standalone variable patterns, you can also add a variable to any other pattern. You simply write the variable name, an at sign (@), and then the pattern. This gives you a variable-binding pattern. The meaning of such a pattern is to perform the pattern match as normal, and if the pattern succeeds, set the variable to the matched object just as with a simple variable pattern.

http://www.artima.com/pins1ed/case-classes-and-pattern-matching.html

Nikita
  • 4,435
  • 3
  • 24
  • 44