When is a scala partial function not a partial function?

Posted by Fred Haslam on Stack Overflow See other posts from Stack Overflow or by Fred Haslam
Published on 2010-05-20T04:34:06Z Indexed on 2010/05/20 4:40 UTC
Read the original article Hit count: 314

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 
}

© Stack Overflow or respective owner

Related posts about scala

Related posts about partialfunction