11

I'm trying to implement a scala function object (specifically - to use with apache spark)

the equivalent java type is scala.Function0 (for a parameter less function)

besides the method that implements the business logic apply(), I see that I have to implement several other methods.

Simple googling the method names didn't help (I saw reference regarding a tag method which should be implemented - but the method name here are different.

Here is the code with those methods (with empty implementation).

    private static class functionObject implements Function0<Integer>{

    @Override
    public Integer apply() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public byte apply$mcB$sp() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public char apply$mcC$sp() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public double apply$mcD$sp() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public float apply$mcF$sp() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int apply$mcI$sp() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long apply$mcJ$sp() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public short apply$mcS$sp() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void apply$mcV$sp() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean apply$mcZ$sp() {
        // TODO Auto-generated method stub
        return false;
    }

}

What are these methods? how are they supposed to be implemented? is there a cleaner solution that adding these to any class?

7
  • Perhaps a stupid question but why are you trying to write a scala function in java? Write in in scala or write a java construct? Commented Sep 2, 2014 at 12:54
  • I'm trying to use apache spark in a java project. spark is a scala project, so it expects scala objects. Our project is in java, so I prefer not to add scala code. Theoretically, java - scala (and specifically, java - spark) interoperability should be relatively simple - but apparently there are some issues that aren't very simple (or at least lack documentation) Commented Sep 2, 2014 at 12:56
  • @Ophiiryoktan Scala -> Java, easy. Java -> Scala; varies. Easy if the scala lib was designed for it (!?!) else a fragile mess of ugliness Commented Sep 2, 2014 at 13:10
  • 3
    Implement AbstractFunctionX Commented Sep 2, 2014 at 13:12
  • 1
    Use Scala, use the write tool for the job rather than sticking to what you feel safe with. When you learn scala you will start hating the verbosity of java and realize you can sack 10 java devs and replace with just 1 scala dev Commented Sep 3, 2014 at 5:08

1 Answer 1

19

Scalas FunctionX interfaces are traits, whose type parameters are specialized. Because specialization is done by scalac and therefore can't be done by javac you have to implement everything related to specialization by yourself.

Scala provides the abstract classes AbstractFunctionX, which can be easily implemented on the Java side:

// example Int => Int function
class Z extends scala.runtime.AbstractFunction1<Integer, Integer> {
  public Integer apply(Integer i) {
    return i;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Great answer ! I did implement similar class (though not specialized) since scala 2.7 without noticing they exists. Do you know when they were introduced ?
@paradigmatic Since Dec 2009: github.com/scala/scala/commit/…
Thank you for sharing your Scala wisdom @kiritsuku! Saved me a bunch of long evening hours :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.