Hi All, I’m learning Scala now. When going through Partial function definition and usage, I understood use of it, but have a question.
In almost all Google link about Scala Partitial function, I can see the function definition has 2 input parameters, but in code block we are using only one (i.e. 1st) or we pass only 1 parameter.
Can someone please help me here to explain this? We can have only one parameter in definition, since we pass only one.
The Google code you are showing is almost never used directly.
We do not create partial functions by directly using PartialFunction[X,Y].
Instead we use case expressions / pattern matching.
The PartialFunction[X,Y] is a mechanism that Scala uses “behind the scenes” to convert case expressions to partial functions with the correct type. Sometimes this is used directly to explain how it works (like the book does in the screenshot).
Anyway, to answer the question, PartialFunction[X,Y] is a function that takes X as input type, and returns Y as output type. That’s why it looks like “two input parameters”. But the second one is actually the type of the output.
Yes probably. You are lacking crucial information.
Type parameters and input parameters are different. This is also explained in the book, Chapter 18, “Type Parameterization”:
So you see, you really have to learn Scala properly from a good source. It’s not like a typical programming language. Some languages don’t even have type parameters.