I am just starting my seemingly steep learning curve with Scala and I can’t understand how the “case” works in partial functions exactly.
I looked at the definition of PartialFunction itself, and there I see a sample like the following:
val isEven: PartialFunction[Int, String] = {
case x if x % 2 == 0 => x+" is even"
}
The part where I'm stuck is case x, if x% 2 - how does Scala know what is here? What is the formal definition of this "case" statement / keyword?
I think one of the reasons for my confusion is that in Lift I see things like the following (in Actor classes):
override def messageHandler = {
case SomeKindOfUserMessageClass(id1, param1) => ....
case AnotherKindOfUserMessageClass(id2) => ....
}
I understand what is going on here intuitively, but I cannot put together some unified definition of how to use the “case”. Even more perplexing for me is how the Scala compiler unravels all this.
source
share