Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a function that requires parameters as a callback in Kotlin

I am using Kotlin (for Android development) and I'm trying to pass a function to another function which I'd like to use as a callback. The code is very basic as this is just a test for now.

Please note that, although you will probably wonder why I'm using a callback like this, it's just for test purposes. In my actual application I would want to assign the callback to a value and call it later on once an asynchronous method has completed.

I cannot use co-routines etc... since this code will be used for a multi-platform solution, hence my interest in making a function callback.

My Kotlin Class that will receive the function (callback)

class SampleApi {
    private var counter: Int = 0

    fun startCounting(initialValue: Int, counterCallBack: (resultVal: Int) -> Unit) {
        counter = initialValue

        counter++

        counterCallBack(counter)
    }
}

The above is a basic class that has a function startCounting which will receive an integer and a function. It will then call that function and pass in a value.

The calling code

class MainActivity : AppCompatActivity() {

    private val sampleApi: SampleApi = SampleApi()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        sampleApi.startCounting(5, {counterCallBack()})
    }

    private fun counterCallBack(counter: Int) {
        Toast.makeText(this, counter.toString(), Toast.LENGTH_SHORT).show()
    }
}

The Sample code shown above contains the callback method (which is expecting to receive an integer), and contains the call to the startCounting method from the SampleApi class that is expecting to receive a function.

The problem I have is this line:

sampleApi.startCounting(5, {counterCallBack()})

The error within Android Studio is due to the fact that a value is that the function is expecting an integer and hence I receive the error:

No value passed for parameter 'counter'

I tried to look at lambdas but didn't think that was the issue. I have searched to see if an answer to this already existed and, whilst helpful they didn't seem to consider the same use case as mine.

Any help with this would be very much appreciated.

like image 300
greysqrl Avatar asked Jul 05 '19 10:07

greysqrl


People also ask

Can you pass functions as parameters in Kotlin?

Kotlin functions are first-class, which means they can be stored in variables and data structures, and can be passed as arguments to and returned from other higher-order functions. You can perform any operations on functions that are possible for other non-function values.

How do you pass parameters in Kotlin?

In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T> ) inside the function body.

How do you pass the function in the method Kotlin?

In Kotlin, a function which can accept a function as parameter or can return a function is called Higher-Order function. Instead of Integer, String or Array as a parameter to function, we will pass anonymous function or lambdas. Frequently, lambdas are passed as parameter in Kotlin functions for the convenience.


1 Answers

Because counterCallback has exactly the type you need, you can also use a function reference instead of a lambda:

sampleApi.startCounting(5, ::counterCallBack)
like image 191
Alexey Romanov Avatar answered Sep 21 '22 13:09

Alexey Romanov