Partial Function, a Scala language concept
Last updated
A Partial Function in Scala is similar to function type `A => Option[B]` (Option Type).
The most useful aspect is PartialFunction.condOpt which allows you to reduce boilerplate when writing pattern matches (Pattern Matching).
For example, this is how it looks if you write a simple pattern match:
And this is how it looks in the PartialFunction code:
Turning it into a complete function
Note if the .apply method is called where it is not defined, an exception will be thrown.