7

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.

1 Answer 1

12

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> {
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.