I want to call the foldLeft method on a scala Buffer, from Java code:
List<Integer> javaList = new ArrayList<>();
javaList.add(1);
javaList.add(3);
javaList.add(5);
Buffer<Integer> scalaBuffer = JavaConversions.asScalaBuffer(javaList);
scalaBuffer.foldLeft(0, new Function2<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer accumulator, Integer element) {
return carry + element;
}
});
But I get the following compiler error:
Multiple markers at this line
- The type new Function2<Integer,Integer,Integer>(){} must implement the
inherited abstract method Function2<Integer,Integer,Integer>.apply$mcVDI
$sp(double, int)
- The type new Function2<Integer,Integer,Integer>(){} must implement the
inherited abstract method Function2<Integer,Integer,Integer>.tupled$mcIDD$sp()
- The type new Function2<Integer,Integer,Integer>(){} must implement the
inherited abstract method Function2<Integer,Integer,Integer>.tupled$mcVJJ$sp()
- The type new Function2<Integer,Integer,Integer>(){} must implement the
inherited abstract method Function2<Integer,Integer,Integer>.apply$mcZDJ
$sp(double, long)
.
.
.
Basically, I have to implement a whole load of methods that don’t interest me.
Is there an easy solution to calling the foldLeft method on a monad, from Java?
Thanks,
John
(Using Scala 2.10-M5 and JDK 1.7)
According to this scala-lang thread, the appropriate course of action is probably to extend
scala.runtime.AbstractFunction2instead of directly implementingFunction2.That thread is a bit old in scala time, but it may point toward a solution. I confess I haven’t tried it myself.