Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a function as parameter in kotlin - Android

How to pass a function in android using Kotlin . I can able to pass if i know the function like :

fun a(b :() -> Unit){
}
fun b(){
}

I want to pass any function like ->
fun passAnyFunc(fun : (?) ->Unit){}

like image 710
Skand kumar Avatar asked Nov 28 '22 06:11

Skand kumar


1 Answers

You can use anonymous function or a lambda as follows

fun main(args: Array<String>) {

    fun something(exec: Boolean, func: () -> Unit) {
        if(exec) {
            func()
        }
    }

    //Anonymous function
    something(true, fun() {
        println("bleh")
    })

    //Lambda
    something(true) {
        println("bleh")
    }

}
like image 198
Yaswant Narayan Avatar answered Dec 17 '22 19:12

Yaswant Narayan