Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Function conversion to Kotlin fails

Tags:

java

kotlin

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

enter image description here

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

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

like image 568
vach Avatar asked Apr 15 '16 10:04

vach


1 Answers

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> {
    // ...
}
like image 84
hotkey Avatar answered Oct 24 '22 10:10

hotkey