Can not use kotlin.Function1 outside of kotlin code

Hi, I’m trying to create kotlin interface for Apache Spark. To do so, I need somehow to match kotlin’s lambda to scala lambda. In scala I have classes: Function1[E,T], Function2[E,T] … I believe in kotlin I have Function1 as well since I was able to write next code:

package hello fun test(f: Function1<String, String>) { println(f.invoke("sfsdf")) } fun main(args: Array<String>) { val getHelloString: (String) -> String = { s -> println(s) s } test(getHelloString) }

But when I try to access this class from Java or Scala code, which I strongly prefer it can not compile.

package hello;
import kotlin.Function1

public class JavaHello {
public static String JavaHelloString = “Hello from Java!”;

public static String getHelloStringFromKotlin() {
return KotlinHelloKt.getKotlinHelloString();
}

public static void main(String args) {
System.out.println(getHelloStringFromKotlin());
System.out.println(KotlinHelloKt.getHelloStringFromJava());

}
}

I got

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/egor/kotlin-examples/maven/mixed-code-hello-world/src/main/java/hello/JavaHello.java:[2,13] error: cannot find symbol
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.979s
[INFO] Finished at: Sun Mar 06 16:07:55 PST 2016
[INFO] Final Memory: 32M/237M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project mixed-code-hello-world: Compilation failure
[ERROR] /Users/egor/kotlin-examples/maven/mixed-code-hello-world/src/main/java/hello/JavaHello.java:[2,13] error: cannot find symbol
[ERROR] → [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] MojoFailureException - Apache Maven - Apache Software Foundation

What am I doing wrong?

Yes, I can map kotlin function to scala function in kotlin code, but I’m confused, why I have to do it this way. I would prefer to do it scala with all implicit magic.

Please use kotlin.jvm.functions.Function1 in Java code

1 Like