For interoperability, I need to pass a Scala PartialFunction from Java code. For Function (Function1 and so on), there is AbstractFunction that I can subclass with an anonymous type, but what would be the easiest way of doing the same for PartialFunction?
In this case, I would be happy to have it being a "complete" function in Java, appearing defined for all values, but typed as a PartialFunction.
As a complement to Chris' answer, in Scala 2.10 you can use this: http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/scala/runtime/AbstractPartialFunction.html
If you can use Twitter Util library, it has a class specifically for this: http://twitter.github.com/util/util-core/target/site/doc/main/api/com/twitter/util/Function.html which is basically the same solution as AbstractPartialFunction.
What I would do here is provide an interface in Java, in some common library (which is not scala-aware):
Then, in scala (i.e. a scala library dependent on the above Java library), convert an implementation of this to a
PartialFunction
:Then your Java code can do this:
If you don't like the
MyPartialFunctions$.MODULE$
syntax, it's possible in the scala library, a Java class which hides this from you:Then your call-site looks like this:
This involves, ahem, a few levels of indirection!