1

How do I create Function1 object for use in flatMap method of finagle's Future object in Java?

Tried this:

Function1<String, String> f = new Function1<String, String>() {
    @Override
    public String apply(String s) {
        return null;
    }
};

But it doesn't work:

Error:(22, 73) java: is not abstract and does not override abstract method andThen$mcVJ$sp(scala.Function1) in scala.Function1

Travis Brown
  • 138,631
  • 12
  • 375
  • 680
user3489275
  • 401
  • 4
  • 8
  • See my answer to a similar question [here](http://stackoverflow.com/a/11679421/334519). You've got it a little easier, since you don't have to worry about the `CanBuildFrom` part, but the `AbstractFunction1` should be exactly what you need. – Travis Brown Apr 02 '14 at 17:09
  • @TravisBrown thanks, it works! Can you please recommend some more reading about Scala/Java interop (such as AbstractFunctionN classes)? – user3489275 Apr 02 '14 at 17:18
  • Twitter's [Scala School lesson on Java interoperability](http://twitter.github.io/scala_school/java.html) is a good place to start. – Travis Brown Apr 02 '14 at 17:23

1 Answers1

0

For the sake of completeness, here's the answer from my two month-old comment above.

First for some imports:

import scala.Function1;
import scala.runtime.AbstractFunction1;

And now you only have to define the apply method:

Function1<String, String> f = new AbstractFunction1<String, String>() {
  public String apply(String s) {
    return s;
  }
};

If you're using Finagle, though, Twitter's Util library also provides a similar helper class:

import com.twitter.util.Function;
import scala.Function1;

And then:

Function1<String, String> f = new Function<String, String>() {
  public String apply(String s) {
    return s;
  }
};

This latter option is probably better—I've never really liked explicitly using stuff from scala.runtime.

Travis Brown
  • 138,631
  • 12
  • 375
  • 680