Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong function conversion documentation #94

Open
choedl opened this issue Aug 30, 2017 · 2 comments
Open

Wrong function conversion documentation #94

choedl opened this issue Aug 30, 2017 · 2 comments

Comments

@choedl
Copy link

choedl commented Aug 30, 2017

I think the documentation regarding function conversion is wrong (at least the java code example) and doesn't compile.

To convert a java function to a scala function (like in baz) I have to write the following:

import static scala.compat.java8.JFunction.*;
...
// instead of return bar(functionConverters.asScalaFromFunction(f)) -> where should functionConverters come from?
return bar(func(f)); 
...

@Rorick
Copy link

Rorick commented Apr 17, 2019

Worked for me this way:

import java.util.function.*;

import scala.compat.java8.FunctionConverters.package$;

import static scala.compat.java8.FunctionConverters.package$.MODULE$;

class Example {
  private static package$ functionConverters = MODULE$;

  String foo(UnaryOperator<String> f) {
    return f.apply("halibut");
  }
  String bar(scala.Function1<String, String> f) {
    return foo(functionConverters.asJavaUnaryOperator(f));
  }
  String baz(Function<String, String> f) {
    return bar(functionConverters.asScalaFromFunction(f));
  }
}

@rdesgroppes
Copy link

rdesgroppes commented Apr 4, 2020

Some more options:

  1. JFunction1
import scala.compat.java8.JFunction1;
...
String baz(Function<String, String> f) {
    return bar((JFunction1<String, String>) f);
}
...
  1. FromJavaFunction
import scala.compat.java8.functionConverterImpls.FromJavaFunction;
...
String baz(Function<String, String> f) {
    return bar(new FromJavaFunction<>(f));
}
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants