Skip to content

Instantly share code, notes, and snippets.

@agemooij
Created June 21, 2012 16:28
Show Gist options
  • Save agemooij/2966846 to your computer and use it in GitHub Desktop.
Save agemooij/2966846 to your computer and use it in GitHub Desktop.
Akka: logging any received message
scala> :paste
// Entering paste mode (ctrl-D to finish)
def wrap(partial: PartialFunction[Any, Unit]): PartialFunction[Any, Unit] = new PartialFunction[Any, Unit] {
def isDefinedAt(a: Any): Boolean = partial.isDefinedAt(a)
def apply(a: Any): Unit = {
println("----> received " + a)
partial.apply(a)
}
}
// Exiting paste mode, now interpreting.
wrap: (partial: PartialFunction[Any,Unit])PartialFunction[Any,Unit]
scala> wrap {
| case i: Int => println("An int!")
| case s: String => println("A String!")
| case _ => println("Something else!")
| }
res0: PartialFunction[Any,Unit] = <function1>
scala> res0(3)
----> received 3
An int!
scala> res0("bla")
----> received bla
A String!
scala> res0(4.89)
----> received 4.89
Something else!
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment