Kotlin functional interfaces java compatiblity

2019-06-22 07:37发布

问题:

I work on an application in kotlin, but need to have a good java support. The problem I found are kotlin's functions.

this is what I used to do

fun test(loader: (String) -> Int)

but this will compile into a Function1 from kotlin library and since I don't have kotlin library directly included in the jar because of jar size, it makes it harder for java developers because they have to download the kotlin library to be able to use this method.

I tried to use Supplier or Function interface from java but I found it a lot more difficult for kotlin developers since you have to provide a lot more variable types and null checks and together with generic arguments it's a pain..

Also tried to create my own interface like

@FunctionalInterface
interface Function<in T, out R>: Function<R> {
    operator fun invoke(p: T): R
}

and function

fun test(loader: Function<String, Int>)

but it's the same as default java Function interface

so the only way that could work is to let the compiler compile my original function to my own functional interface instead of kotlin's one. But I don't have idea how to do that.

回答1:

The problem you're facing is due to missing SAM conversions, see [1], [2] for more information. In short, Java allows you to treat interfaces with one non-default, non-static method as functional interfaces. If this conversion were present in Kotlin, Kotlin lambda expressions could be implicitly converted to Java functional interfaces such as Function<T, R>.

It's not possible to compile function literals to your own functional interface without changes to the compiler.

Your best bet given the status quo is to write a few conversion functions, which can be done very compactly in Kotlin:

object Functional
{
    @JvmStatic fun <T> toKotlin(supplier: Supplier<T>): () -> T = supplier::get
    @JvmStatic fun <T, R> toKotlin(function: Function<T, R>): (T) -> R = function::apply
    @JvmStatic fun <T> toKotlin(function: BinaryOperator<T>): (T, T) -> T = function::apply
    @JvmStatic fun <T> toKotlin(consumer: Consumer<T>): (T) -> Unit = consumer::accept
    ...
}

Let's apply this to a file Example.kt:

// passes an argument to the function and returns the result
fun call(function: (Int) -> Int, arg: Int): Int = function(arg)

From Java, you can then use them as follows:

import static yourpackage.Functional.toKotlin;

// in code:
ExampleKt.call(toKotlin(x -> 3 * x), 42);

Of course, if convenience is your goal, then you can overload your methods taking function parameters, to support both Kotlin and Java ways:

// Kotlin:
fun call(function: (Int) -> Int, arg: Int): Int = function(arg)
fun call(function: Function<Int, Int>, arg: Int): Int = call(toKotlin(function), arg)

// Java:
ExampleKt.call(x -> 3 * x, 42);


回答2:

it makes it harder for java developers because they have to download the kotlin library to be able to use this method

JVM developers use build systems such as Maven or Gradle. Trying to manage dependencies manually is just a bad idea. And if any Kotlin-specific types are used in the implementation, the Kotlin standard library is still required.