How can I check if lambda is empty in Kotlin? For example, I have signature like
onError:(Throwable) -> Unit = {}
How can I differ is its default value comes to body or value applied to this function?
How can I check if lambda is empty in Kotlin? For example, I have signature like
onError:(Throwable) -> Unit = {}
How can I differ is its default value comes to body or value applied to this function?
You couldn't test if the body of an lambda is empty (so it contains no source-code) but you can check if the lambda is your default value by creating a constant for that value and use this as default value. Than you can also check if the value is the default value:
fun main(args: Array<String>) {
foo()
foo { }
foo { println("Bar") }
}
private val EMPTY: (Throwable) -> Unit = {}
fun foo(onError: (Throwable) -> Unit = EMPTY) {
if (onError === EMPTY) {
// the default value is used
} else {
// a lambda was defined - no default value used
}
}