0

I have a Kotlin function fun myFun(book:Book){...}. How do I pass myFun to a function that accepts a lambda?

fun acceptor(task: (book:Book) -> Unit){ var someBook = ... ... task(someBook) }

1 Answer 1

2
class MyClass {

    fun myFun(book:Book) {
        ...
    }

    fun otherThatCallsAccept() {
        /* other stuff */
        acceptor(::myFun)
    }
}

Edit: you can't call acceptor without a parameter, but you can define a default empty lambda,

fun acceptor(task: (book:Book) -> Unit = {}) {
    /* body here */
}
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to call acceptor without a parameter?
btw thanks for understanding the question. Other people I ask keep thinking I am talking about passing a function as a parameter -- even Google searches found nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.