Home > database >  How to assign a default value to a function as parameter in Kotlin
How to assign a default value to a function as parameter in Kotlin

Time:05-05

In my Kotlin project I would like to assign a default value to a function passed as a parameter, something like:

fun myFun(book: String, isCool: (book: String) -> Boolean = _ -> true) {
    if (isCool(book)) println("$book is cool!")
}

Is this possible?

CodePudding user response:

Yes it is possible

fun myFun(book: String, isCool: (book: String) -> Boolean = { _ -> true }) {
    if (isCool(book)) println("$book is cool!")
}
  • Related