英文:
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<T, R>
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<T, R>
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<String, String> function;
public A(Function<String, String> 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<T, R>
, 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<String, Int>
or
val function = fun (x: String): Int {
return x.length
}
// Java code will treat this as a Function1<String, Int>
or
val functionReference = { x: String -> x.length }
// Java code will treat this as a Function1<String, Int>
To declare that a function takes a function as a parameter, you use (input) -> output
syntax as the variable type:
fun <T, R> doSomething(functionalReference: (T) -> R) {
//
}
You can call a function using its referenced name:
fun <T, R> doSomething(input: T, functionalReference: (T) -> 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 <T,R> void runAFunction(Function<T, R> userFunction){
userFunction.apply(null);
}
}
Kotlin (These two functions are equivalent)
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);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论