Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在多平台项目中使用Kotlin正确处理SAM功能?_Java_Kotlin_Multiplatform - Fatal编程技术网

Java 如何在多平台项目中使用Kotlin正确处理SAM功能?

Java 如何在多平台项目中使用Kotlin正确处理SAM功能?,java,kotlin,multiplatform,Java,Kotlin,Multiplatform,我正在从事一个多平台项目,并试图公开一个API来添加侦听器: interface InputEmitter { fun addInputListener(listener: InputListener) } InputListener是一个SAM接口,它看起来如下: interface InputListener { fun onInput(input: Input) } obj.addInputListener(object : InputListener {

我正在从事一个多平台项目,并试图公开一个API来添加侦听器:

interface InputEmitter {

    fun addInputListener(listener: InputListener)

}
InputListener
是一个SAM接口,它看起来如下:

interface InputListener {

    fun onInput(input: Input)
}
obj.addInputListener(object : InputListener {
    override fun onInput(input: Input) {
        TODO("not implemented")
    }
})
fun addInputListener(listener: InputListener)

fun onInput(listener: (Input) -> Unit)
我的问题是,这在Java和Kotlin中都能正常工作,但在Kotlin方面这不是惯用的,因为我必须这样调用它:

interface InputListener {

    fun onInput(input: Input)
}
obj.addInputListener(object : InputListener {
    override fun onInput(input: Input) {
        TODO("not implemented")
    }
})
fun addInputListener(listener: InputListener)

fun onInput(listener: (Input) -> Unit)
与此相反:

obj.addInputListener {
    TODO("not implemented")
}
在这种情况下,编译器会抱怨类型不匹配,这是正常的

我可以通过使用
@functionanterface
注释来解决这个问题,在这种情况下,Kotlin端会更好,但由于这是一个多平台项目,我不能使用该注释

增加两个功能:

fun addInputListener(listener: InputListener)

fun addInputListener(listener: (Input) -> Unit)
也不起作用,因为它在Java方面是不明确的。我如何解决这个问题,使它在Java和Kotlin中都能正常工作

我知道我可以这样做:

interface InputListener {

    fun onInput(input: Input)
}
obj.addInputListener(object : InputListener {
    override fun onInput(input: Input) {
        TODO("not implemented")
    }
})
fun addInputListener(listener: InputListener)

fun onInput(listener: (Input) -> Unit)
但是在这种情况下,当Java用户想要使用
onInput
变量时,他/她会感到困惑(因为它解析为
Function1
,这在Java端是不可实例化的)


这个问题有一个规范的解决方案吗?

您如何使用
@functioninterface
解决这个问题?Kotlin不支持Kotlin定义的接口的SAM,无论它们是如何注释的。我可以添加一个Java
接口
,该接口具有注释,然后我可以像这样使用它:
addInputListener(InputListener{//do something})
这仍然不是完美的,但比另一个更好。我当前的解决方案是一个扩展函数,它不会污染Java名称空间,但使用此选项,我有两个版本的
addInputListener
:一个是您上面看到的,另一个是扩展函数,使用lambda。有更好的解决方案吗?在Java接口中定义API并在Kotlin中进行实现将使您能够用两种语言进行sam转换。您可以将onInput方法重写为一个扩展函数,那么它在Java中就不可见了。