I am an absolute Scala novice. This question is therefore very simplistic, and hopefully someone will understand what is being asked.
When experimenting, I have seen I can create a PartialFunction instance with the following code:
val p : PartialFunction[Int, String] = {case x if x > 2 => s"x is ${x.toString}"}
My question: How is a concrete PartialFunction[Int, String] created from the function {case x if x > 2 => s"x is ${x.toString}"}?
In particular, how does this function provide both the..
isDefinedAt(x: Int): Booleanmethod definition
..and the..
apply(v1: Int): Stringmethod definition
..that a concrete PartialFunction[Int, String] must have?
Behind the scenes, is {case x if x > 2 => s"x is ${x.toString}"} being turned into?:
val p : PartialFunction[Int, String] = new PartialFunction[Int, String] {
override def apply(v1: Int): String = {
v1 match {
case x if x > 2 => s"x is ${x.toString}"
}
}
override def isDefinedAt(x: Int): Boolean = {
x match {
case x if x > 2 => true
case _ => false
}
}
}