简体   繁体   中英

Java Function conversion to Kotlin fails

Trying to convert some java code to kotlin, given the following method

public class Option<T> {

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

kotlin conversion will give this

在此输入图像描述

I cannot understand whats the problem here, and how do i create equivalent method in kotlin? (thats the java.util.Function )

PS could not come up with some better question summary... feel free to change.

To use java.util.function.Function , you have to import it explicitly:

import java.util.function.Function

That's because by default Function is resolved to kotlin.Function .

But there are function types in Kotlin, and more idiomatic implementation would be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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