Kotlin – 找不到与 java.util.function.Function 等价的 Kotlin 版本。

huangapple go评论111阅读模式
英文:

Kotlin - not able to find kotin equivalent of java.util.function.Function<T, R>

问题

I am trying to convert one of the java function which takes java.util.function.Function&lt;T, R&gt; to Kotlin using IDEA 2019.3 Kotlin multiplatform library.

But t I could not find a way to do an equivalent function in kotlin. I can see here that there is Function1 to do a java interoperability but I am not able to do any import from import kotlin.jvm.functions.*

I am trying Kotlin for the first time. Could someone please tell what am I doing wrong.

Update- Please see my java code

import java.util.function.Function

class A(private val function: Function<String, String>) {

    fun convert(input: String): String {
        return function.apply(input)
    }
}
英文:

I am trying to convert one of the java function which takes java.util.function.Function&lt;T, R&gt; to Kotlin using IDEA 2019.3 Kotlin multiplatform library.

But t I could not find a way to do an equivalent function in kotlin. I can see here that there is Function1 to do a java interoperability but I am not able to do any import from import kotlin.jvm.functions.*

I am trying Kotlin for the first time. Could someone please tell what am I doing wrong.

Update- Please see my java code

import java.util.function.Function;

public class A {
	Function&lt;String, String&gt; function;

	public A(Function&lt;String, String&gt; function) {
		super();
		this.function = function;
	}
	
public String convert(String input) {
	return function.apply(input);
	
}
}

答案1

得分: 2

不清楚你的问题,但假设你正在尝试复制Java中的功能:

在Kotlin中,你不直接使用Function接口,因为函数是头等公民。Function1、Function2等类仅用于使函数在Java代码和JVM中可用。

如果你想创建与Java的Function<T, R>相等的函数,你可以使用Kotlin的fun或lambda语法来定义函数。

fun getStringLength(x: String): Int {
    return x.length
}

//...
val functionReference = ::getStringLength
// Java代码将把此视为Function1<String, Int>;

或者

val function = fun (x: String): Int {
    return x.length
}
// Java代码将把此视为Function1<String, Int>;

或者

val functionReference = { x: String -> x.length }
// Java代码将把此视为Function1<String, Int>;

要声明一个函数接受一个函数作为参数,你可以使用(input) -> output语法作为变量类型:

fun <T, R> doSomething(functionalReference: (T) -> R) {
    //
}

你可以使用函数的引用名称来调用函数:

fun <T, R> doSomething(input: T, functionalReference: (T) -> R): R {
    return functionalReference(input)
}
英文:

Not clear about your question, but assuming you are trying to duplicate functionality from Java:

In Kotlin, you do not use Function interfaces directly because functions are first-class. The Function1, Function2, etc. classes are only used to make functions available to Java code and the JVM.

If you want to create the equivalent of a Java Function&lt;T, R&gt;, you would define a function using either Kotlin's fun or lambda syntax.

fun getStringLength(x: String): Int {
    return x.length
}

//...
val functionReference = ::getStringLength
// Java code will treat this as a Function1&lt;String, Int&gt;

or

val function = fun (x: String): Int {
    return x.length
}
// Java code will treat this as a Function1&lt;String, Int&gt;

or

val functionReference = { x: String -&gt; x.length }
// Java code will treat this as a Function1&lt;String, Int&gt;

To declare that a function takes a function as a parameter, you use (input) -&gt; output syntax as the variable type:

fun &lt;T, R&gt; doSomething(functionalReference: (T) -&gt; R) {
    //
}

You can call a function using its referenced name:

fun &lt;T, R&gt; doSomething(input: T, functionalReference: (T) -&gt; R): R {
    return functionalReference(input)
}

答案2

得分: 1

以下是翻译后的内容:

听起来你想将一个接受 Function 的 Java 函数转换为等效的 Kotlin 函数。

示例:

Java

public class JavaFunctions {
   public static <T,R> void runAFunction(Function<T, R> userFunction){
      userFunction.apply(null);
   }
}

Kotlin(这两个函数是等效的)

class KotlinFunctions{
   companion object{
      @JvmStatic
      fun <T,R> runAFunction(userFunction: (T?) -> R?){
         userFunction.invoke(null);
      }

      @JvmStatic
      fun <T,R> runAFunction2(userFunction: Function1<T?, R?> ){
         userFunction.invoke(null);
      }
   }
}

请注意,代码部分未进行翻译。

英文:

Sounds like you want to convert a Java function that accepts a Function to an equivalent Kotlin function.

Example:

Java

public class JavaFunctions {
   public static &lt;T,R&gt; void runAFunction(Function&lt;T, R&gt; userFunction){
      userFunction.apply(null);
   }
}

Kotlin (These two functions are equivalent)

class KotlinFunctions{
   companion object{
      @JvmStatic
      fun &lt;T,R&gt; runAFunction(userFunction:(T?) -&gt; R?){
         userFunction.invoke(null);
      }

      @JvmStatic
      fun &lt;T,R&gt; runAFunction2(userFunction: Function1&lt;T?, R?&gt; ){
         userFunction.invoke(null);
      }
   }
}

huangapple
  • 本文由 发表于 2020年1月6日 23:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定