Q: Kotlin lambda function with arguments I am currently developing an Android application that uses the Retrofit library for REST api usage.
For instance, I have the following code from MainActivity.kt :
fun userLogin(){
    calls.userLogin() { updateUiComponents() }
    }

fun updateUiComponents(){
    Toast.makeText(applicationContext, "LAMBDA EXECUTED",Toast.LENGTH_SHORT).show()
    }

And I have in a separate file the definition of the Retrofit calls:
fun userLogin(postActionMethod: () -> Unit){
    val call = service.userLogin()
    call.enqueue(object : Callback<LoginResponse>{
        override fun onFailure(call: Call<LoginResponse>?, t: Throwable?) {
            Log.i("ERROR RUNNING CALL", t?.message.toString())
        }

        override fun onResponse(call: Call<LoginResponse>?, response: Response<LoginResponse>?) {
            postActionMethod()
        }
    })
}

After the Retrofit call is implemented and it is successful, reaching the onResponse method, I would like to send the Response object as a parameter of the lambda function back to the MainAcativity.kt. From the MainActivity.kt, the lambda function would use this information to perform some specific task.
Is that a way of defining a lambda function like this, with arguments? If it is the case, how can I pass the lambda function as a parameter like done on the following line:
calls.userLogin(body) { updateUiComponents() }

Thank u!

A: I don't know if I get what your problem is but a lambda does not need to do not have any parameter. You can easily do something like
fun userLogin(postActionMethod: (Response<LoginResponse>?) -> Unit){
    val call = service.userLogin()
    call.enqueue(object : Callback<LoginResponse>{
        override fun onFailure(call: Call<LoginResponse>?, t: Throwable?) {
            Log.i("ERROR RUNNING CALL", t?.message.toString())
        }

        override fun onResponse(call: Call<LoginResponse>?, response: Response<LoginResponse>?) {
            postActionMethod(response)
        }
    })
}

so you consume it with
fun userLogin(){
    calls.userLogin() { updateUiComponents(it) }
    }

fun updateUiComponents(response: Response<LoginResponse>?){
    Toast.makeText(applicationContext, "LAMBDA EXECUTED",Toast.LENGTH_SHORT).show()
    }

