... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2008-01-09

Scala Partial Functions

In Scala, a block that contains a series of case expressions defines a partial function, thus:

val x: PartialFunction[Any,Int] = {case x: String => 1 case _: Any => 2}

Note that the type of x in the example must be specified because the input parameter to the partial function cannot be inferred.  This would not be necessary in circumstances where there is enough information to infer the type, e.g.

def doit(f: PartialFunction[Any, Int]) = f("hi")

doit({case x: String => 1 case _: Any => 2})

Blog Archive