简体   繁体   English

Java函数转换为Kotlin失败

[英]Java Function conversion to Kotlin fails

Trying to convert some java code to kotlin, given the following method 尝试将一些Java代码转换为kotlin,给出以下方法

public class Option<T> {

  public <U> Option<U> map(Function<T, U> mapper) {
    throw new IllegalStateException();
  }
}

kotlin conversion will give this kotlin转换会给出这个

在此输入图像描述

I cannot understand whats the problem here, and how do i create equivalent method in kotlin? 我无法理解这里的问题是什么,我如何在kotlin中创建等效方法? (thats the java.util.Function ) (那就是java.util.Function

PS could not come up with some better question summary... feel free to change. PS无法提出一些更好的问题摘要......随意改变。

To use java.util.function.Function , you have to import it explicitly: 要使用java.util.function.Function ,必须显式导入它:

import java.util.function.Function

That's because by default Function is resolved to kotlin.Function . 那是因为默认情况下, Function被解析为kotlin.Function

But there are function types in Kotlin, and more idiomatic implementation would be 但是Kotlin中有一些函数类型 ,更多的是惯用的实现

fun <U> map(mapper: (T) -> U): Option<U> {
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM