When is a scala partial function not a partial function?
function, functional-programming, partial-application, partialfunction, scala
Solution
For convenience, Scala lets you omit empty parens when calling a method, but it's clever enough to see that the expected type in the first case is `()=>Unit`, so it doesn't remove all the parens for you; instead, it converts the method into a function for you.
In the `val check` case, however, it looks just like a function call result getting assigned to a variable. In fact, all three of these do the exact same thing:
val check = dream
val check = dream()
val check = dream()()
If you want to turn the method into a function, you place `_` after the method in place of the argument list(s). Thus,
val check = dream() _
will do what you want.
Problem
While creating a map of String to partial functions I ran into unexpected behavior. When I create a partial function as a map element it works fine. When I allocate to a val it invokes instead. Trying to invoke the check generates an error. Is this expected? Am I doing something dumb? Comment out the `check()` to see the invocation. I am using scala 2.7.7 ``` def PartialFunctionProblem() = { def dream()() = { println("~Dream~"); new Exception().printStackTrace() } val map = scala.collection.mutable.HashMap[String,()=>Unit]() map("dream") = dream() // partial function map("dream")() // invokes as expected val check = dream() // unexpected invocation check() // error: check of type Unit does not take parameters } ```