Is that possible to somehow marshall PartialFunction (let's assume it will always contains only one case) into something human-readable?
Let's say we have collection of type Any (messages: List[Any]) and number of PartialFuntion[Any, T] defined using pattern matching block.
case object R1
case object R2
case object R3
val pm1: PartialFunction[Any, Any] = {
case "foo" => R1
}
val pm2: PartialFunction[Any, Any] = {
case x: Int if x > 10 => R2
}
val pm3: PartialFunction[Any, Any] = {
case x: Boolean => R3
}
val messages: List[Any] = List("foo", 20)
val functions = List(pm1, pm2)
then we can find all the messages matched by provided PFs and related applications
val found: List[Option[Any]] = functions map { f =>
messages.find(f.isDefined).map(f)
}
but what if I need resulting map of 'what I expect' to 'what I've got' in the human-readable form (for logging). Say,
(case "foo") -> Some(R1)
(case Int if _ > 10) -> Some(R2)
(case Boolean) -> None
Is that possible? Some macro/meta works?
Thanks for your answers. Using Macro is interesting one choice. But as an option the solution might be to use kind of named partial functions. The idea is to name the function so in the output you can see the name of function instead source code.
So then you can use it as follows