Call a method with a Kotlin callback block from Java

When executing a method with a kotlin callback block from Java code, it was necessary to do the following. In the following cases, on the Java side return Unit.INSTANCE; If there is no compilation error, I was wondering what to do just because I was told that the return type of the ramda expression is inconsistent, so I posted it.

kotlin



class KotlinClass {

    fun loadItems(itemIds: List<String>,
                  complete: (isSuccess: Boolean, itemList: List<Item>) -> Unit) {
        /*Some processing*/
        complete(true, itemList)
    }
}


java (caller)



kotlinClass.loadItems(itemIds, (isSuccess, items) -> {
                    /*Some processing*/

                    //Note: Finally, Unit.Must return INSTANCE
                    return Unit.INSTANCE;
                })

Reason

In kotlin, even if there is no return value of the method, it is actually treated as omitting the return Unit, and it seems that you have to do return Unit.INSTANCE; in order to maintain compatibility with it in Java.

Reference: http://stackoverflow.com/questions/37828790/why-do-i-have-to-return-unit-instance-when-implementing-in-java-a-kotlin-functio

Recommended Posts

Call a method with a Kotlin callback block from Java
[Kotlin] Get Java Constructor / Method from KFunction and call it
Call Java library from C with JNI
Call Java method from JavaScript executed in Java
[Android] Call Kotlin's default argument method from Java
Java method call from RPG (method call in own class)
I tried hitting a Java method from ABCL
I tried to break a block with java (1)
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Call Java from JRuby
Write a class in Kotlin and call it in Java
[Note] Create a java environment from scratch with docker
Call a program written in Swift from Processing (Java)
Create a java method [Memo] [java11]
[ruby] Method call with argument
[Java] Get Charset with Apathce Tika / Initialize String from Charset [Kotlin]
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Take a look at Kotlin from an Effective Java perspective
Call Kotlin's sealed class from Java
Search and execute method by name from instance with processing (java)
Build a Java project with Gradle
Run a batch file from Java
Connecting to a database with Java (Part 1) Maybe the basic method
Access Teradata from a Java application
A simple sample callback in Java
Work with Google Sheets from Java
Call TensorFlow Java API from Scala
Call a C function from Swift
[Gradle] Build a Java project with a configuration different from the convention
Call the super method in Java
What I don't like when using interface of a function with default arguments in Kotlin from Java
Declare a method that has a Java return value with the return value data type
[Kotlin] Handle Java fields from Kotlin safely with Not Null (Non Null) / Nullable annotation [Java]
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
Call GitHub API from Java Socket API part2
API integration from Java with Jersey Client
[Java] How to compare with equals method
Concurrency Method in Java with basic example
Create a MySQL environment with Docker from 0-> 1
Getting Started with Java Starting from 0 Part 1
Try running a Kubernetes Job from Java
Why does Java call a file a class?
A Java engineer compared Swift, Kotlin, and Java.
Directory change monitoring method memo (Java, Kotlin)
I made a rock-paper-scissors app with kotlin
Call Java methods from Nim using jnim
[Kotlin] Delete files with duplicate contents [Java]
Split a string with ". (Dot)" in Java
Interoperability tips with Kotlin for Java developers
Use a named format with Ruby's format method
Memo for migration from java to kotlin
Execute Java code from cpp with cocos2dx
If a person from Java learns PHP
[Kotlin] I wanted to generate a png with a large capacity per area [Java]
Java method
java (method)
Increment with the third argument of iterate method of Stream class added from Java9
Java method
[Java] method
[Java] method