0

I'm trying to use the scaffolding described by oxbow_lakes in What is the easiest way to implement a Scala PartialFunction in Java? to call a Scala method that takes a PartialFunction. Here's my Scala code:

class MyScalaClass {
   def instanceOfSomeType: SomeType = ...

   def consume(processor: PartialFunction[SomeType, Unit]) {
      processor.lift(instanceOfSomeType)
   }
}

And here's how I'm trying to call it from Java:

  MyScalaClass myScalaClass = new MyScalaClass();

  PartialTransformer<SomeType, Unit> fn = new PartialTransformer<SomeType, Unit>() {
     @Override public boolean isDefinedAt(SomeType input) { return true; }
     @Override protected Unit transform0(SomeType input) { return null; }
  };

  PartialFunction<SomeType, Unit> partial = PartialFunctionBridge$.MODULE$.fromPartialTransformer(fn);

  myScalaClass.consume(partial);

The compiler tells me:

consumeInt(scala.PartialFunction<SomeType,java.lang.Object>) in MyScalaClass cannot be applied
to        (scala.PartialFunction<SomeType,scala.Unit>)
Community
  • 1
  • 1
Josh Glover
  • 25,142
  • 27
  • 92
  • 129

1 Answers1

0

Use void as return types in Java to correspond to Unit in Scala. Use scala.Unit for actual type parameters in generics.

Update

I was wrong. You cannot use void on the Java side to correspond to Unit on the Scala side. That's another fiction Scala provides in the reverse direction.

Working on it...

Update 2

It seems a lot of methods marked concrete in PartialFunction are created by the compiler. This is proving a lot more tedious than I thought and work beckons.

As usual, accessing Scala from Java is a lot harder than the reverse. Perhaps you can create a Java-friendly API on your Scala side?

Randall Schulz
  • 26,420
  • 4
  • 61
  • 81