0

I was looking at ActorLogging here and came across this syntax:

def receive = LoggingReceive {
   case x => ...
}

What does this syntax SomeName { } mean? I know that in scala {} create a block of statements and the last line is assigned the variable. The comments explain it as :

Wrap a Receive partial function in a logging enclosure

Is there a technical term for it so that I can learn its usage more? Note: I know what a partial function is. From the comments and answers I understood that LoggingReceive returns a partial function and the syntax of apply.

codingsplash
  • 4,785
  • 12
  • 51
  • 90
  • Possible duplicate of [Using partial functions in Scala - how does it work?](http://stackoverflow.com/questions/8650549/using-partial-functions-in-scala-how-does-it-work) – zhelezoglo Feb 05 '17 at 10:07
  • Also read this: [What is the formal difference in Scala between braces and parentheses, and when should they be used?](http://stackoverflow.com/questions/4386127/what-is-the-formal-difference-in-scala-between-braces-and-parentheses-and-when) – zhelezoglo Feb 05 '17 at 10:13

1 Answers1

-1

In akka the receive method has to have Receive as the result type. So here they're using an object LoggingReceive which has an apply method defined like this:

def apply(r: Receive)(implicit context: ActorContext): Receive = withLabel(null)(r)

In Scala we have a syntax sugar construct so instead of calling:

LoggingReceive.apply(...)

You can simply write:

LoggingReceive(...)

There's another thing - in Scala we can use {...} brackets instead of (...) parentheses, so the above expression can be write as:

LoggingReceive{...}

So at the end they're just simply wrapping the receive method with the apply method of LoggingReceive which calls the withLabel() method.

Porcupine
  • 324
  • 3
  • 8
  • This actually doesn't explain the feature the original poster looked for. Please see http://stackoverflow.com/questions/4386127/what-is-the-formal-difference-in-scala-between-braces-and-parentheses-and-when for the real explanation. See the "Function/Partial Function literals with case" section of the answer. – jrudolph Feb 07 '17 at 12:34