2012-07-25 30 views
0

在緩衝區調用foldLeft我想打電話給在斯卡拉BufferfoldLeft方法,從Java代碼:從Java

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; 
     } 
    }); 

,但我得到以下編譯器錯誤:

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) 
    . 
    . 
    . 

基本上,我必須實施一些我不感興趣的方法。

有沒有簡單的解決方案,調用foldLeft方法上的單子,從Java?

感謝, 約翰

(使用Scala的2.10-M5和JDK 1.7)

回答

1

this scala-lang thread,適當的行動當然是可能,而不是延長scala.runtime.AbstractFunction2直接實現Function2

該線程是Scala時間有點老了,但它可能對解決點。我承認我沒有自己嘗試過。

+1

你贏了。兩個答案都是正確的。你有一個包名和一個鏈接,所以它稍微好一些。 – 2012-07-25 22:59:26

1

你可以嘗試擴大AbstractFunction2,而不是實施Function2

對於Java編譯器,Scala的特徵是隻有接口,也沒有辦法它會自動讓你的那些沒有在他們的特性抽象方法的實現。只看該接口,所有的方法都是從java中看到的抽象的。