Sometimes, when I create a method, I need an extractor parameter for pattern matching, while there is not a generic extractor trait in the Scala Standard Library. So we have to use a PartialFunction as the parameter instead.
When I use the PartialFunction, I had to create pattern matching expression like this.
input match {
case x if partialFunction.isDefined(x) =>
val data = partialFunction.apply(x)
doSomethingWith(data)
}
This approach is not perfect because:
I think the better approach is converting the PartialFunction to an extractor and matching with that extractor
implicit final class PartialFunctionAsExtractor[A, B](pf: PartialFunction[A, B]) {
object Extractor {
def unapply(a: A) = PartialFunction.condOpt(a)(pf)
}
}
input match {
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to a topic in the Google Groups "scala-language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-language/g0-hbN5qerQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Here's some discussion of this issue: http://stackoverflow.com/questions/5902266/inverse-of-partialfunctions-lift-method
You received this message because you are subscribed to a topic in the Google Groups "scala-language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-language/g0-hbN5qerQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.