Get Kotlin presets in java

Asked

Viewed 45 times

0

Companywebclient.kt

    fun find(cnpj: String,
         success: (company: Company) -> Unit,
         failure: (throwable: Throwable) -> Unit,
         finished: () -> Unit) {

    val call = retrofit.companyService().find(cnpj, 1)
    call.enqueue(callback(retrofit,
            {
                it?.body()?.let(success)
                finished()
            },
            {
                it?.let(failure)
                finished()
            }))
}

Configactivity.java

                CompanyWebClient webclient =  new CompanyWebClient();                                                
            webclient.find(edCnpj.getText().toString(),                                                          
                company -> {                                                                                     
                    return  Company -> company;                                                                  
                },                                                                                               
                {                                                                                                
                    cnpj_Error = this.message                                                                    
                },                                                                                               
                {                                                                                                
                    closeProgress                                                                                
                }                                                                                                
            );           

how can I make the Success return?

1 answer

1


The first problem is that the lambda functions in Kotlin do not return a void itself but a Unit. The Unit is a concrete object, and therefore, when making the call in Java, it is necessary to return also the Unit object.

The second problem is that in Java it is not possible to change a variable outside the scope of the lambda function. Therefore, the variable cnpj_Error cannot be changed from within the lambda function (unless it is a class variable).

The third problem is how lambda functions are being declared in Java. You need to follow the following rule:

() -> "valor de retorno"
arg -> arg.getName()
(arg1, arg2) -> System.out.println(arg1 + " " + arg2)

Or for multiple lines:

() -> {
   // múltiplas linhas de código
   return <valor de retorno>;
}

A Java build code from your example is as follows:

webclient.find(cnpj,
        company -> {
            processCompany(company);
            return Unit.INSTANCE;
        },
        throwable -> {
            processError(throwable);
            System.out.println(throwable.getMessage());
            return Unit.INSTANCE;
        },
        () -> {
            closeProgress();
            return Unit.INSTANCE;
        }
);
  • thank you, helped a lot in understanding

Browser other questions tagged

You are not signed in. Login or sign up in order to post.