简体   繁体   中英

“Type mismatch” when migrating lambda method from Java to Kotlin

I'm migrating a class from Java to Kotlin but Kotlin converter doesn't convert lambda expressions for me correctly.

Here's my method in Java:

public static <V> void someMethod(Creator<SomeClass, V> creator) {

Here's my Creator interface:

public interface Creator<SomeClass, V> {
    T getPathFromUri(O o);
}

Here's how I implement it in Java:

someMethod(someVariable -> ClassA.methodA(someVariable.methodB()));

However, when I convert this to Kotlin, I get:

someMethod( { someVariable -> ClassA.methodA(someVariable.methodB()) })

But the error I get is:

Type mismatch.
Required: Creator<SomeClass, V>
Found: (???) -> ???

I don't understand what part of it has the wrong syntax. I tried to implement it multiple ways from the suggestions on other posts but I still get errors.

Just when I had given up to post the question to StackOverflow, I found the solution: We have to explicitly call the type before the method, like so:

someMethod( Creator<SomeClass, V> { someVariable -> ClassA.methodA(someVariable.methodB()) })

instead of:

someMethod( { someVariable -> ClassA.methodA(someVariable.methodB()) })

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