1.Definition
Partial functions are partial in the sense that they aren’t defined for all possible inputs, only those inputs that match at least one of the specified case
clauses.
Only case
clauses can be specified in a partial function and the entire function must be enclosed in curly braces. In contrast, “regular” function literals can be wrapped in parentheses or curly braces.
If the function is called with an input that doesn’t match one of the case clauses, a MatchError
is thrown at runtime.
code example
package com.brown
/**
* Created by BrownWong on 2016/9/29.
*/
object Hello {
def main(args: Array[String]): Unit = {
val pf: PartialFunction[Any, String] = {case s:String => "Yes"}
println(pf("HAHA"))
println(pf(12))
}
}
output
Yes
Exception in thread "main" scala.MatchError: 12 (of class java.lang.Integer)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:253)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:251)
at com.brown.Hello$$anonfun$1.applyOrElse(Hello.scala:8)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
at com.brown.Hello$.main(Hello.scala:10)
at com.brown.Hello.main(Hello.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
2.isDefinedAt()
method
You can test if a Partial Function will match an input using the isDefinedAt
method. This function avoids the risk of throwing a MatchError
exception.
code example
package com.brown
/**
* Created by BrownWong on 2016/9/29.
*/
object Hello {
def main(args: Array[String]): Unit = {
val pf: PartialFunction[Any, String] = {case s:String => "Yes"}
println(pf.isDefinedAt("HAHA"))
println(pf.isDefinedAt(12))
}
}
output
true
false
3.“chain” Partial Functions
You can “chain” PartialFunctions together: pf1 orElse pf2 orElse pf3 ….
If pf1
doesn’t match, then pf2
is tried, then pf3
, etc. A MathError
is only thrown if none of them matches.
code example
package com.brown
/**
* Created by BrownWong on 2016/9/29.
*/
object Hello {
def main(args: Array[String]): Unit = {
val pf1: PartialFunction[Any, String] = {case s:String => "Yes"}
val pf2: PartialFunction[Any, String] = {case s:Int => "Yes"}
val pf3 = pf1 orElse pf2
println(pf3.isDefinedAt("HAHA"))
println(pf3.isDefinedAt(12))
}
}
output
true
true
Ref