我正在尝试使用 oxbow_lakes 中描述的脚手架,在Java中实现 Scala PartialFunction 的最简单方法是什么?调用采用 PartialFunction 的 Scala 方法。这是我的 Scala 代码:
class MyScalaClass {
def instanceOfSomeType: SomeType = ...
def consume(processor: PartialFunction[SomeType, Unit]) {
processor.lift(instanceOfSomeType)
}
}
这就是我试图从 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);
编译器告诉我:
consumeInt(scala.PartialFunction<SomeType,java.lang.Object>) in MyScalaClass cannot be applied
to (scala.PartialFunction<SomeType,scala.Unit>)