7

I am just starting my seemingly steep learning curve with Scala and can't quite grasp how "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 am getting stuck is case x if x%2 -- how does Scala know what's what here? What is a formal definition of this "case" statement/keyword?

I think one reason for my confusion is because in Lift I see things like the following(in Actor classes):

override def messageHandler = {
   case SomeKindOfUserMessageClass(id1, param1) => ....
   case AnotherKindOfUserMessageClass(id2) => ....
}

I sort of understand intuitively what's going on here, but I can't assemble some kind of unified definition of how "case" should be used. Even more puzzling to me is how Scala compiler untangles all that.

4

4 に答える 4