vach
vach

Reputation: 11387

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

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.

Upvotes: 7

Views: 2547

Answers (1)

hotkey
hotkey

Reputation: 148149

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> {
    // ...
}

Upvotes: 12

Related Questions