How to check lambda emptiness in Kotlin

How to check if there is a lambda in Kotlin? For example, I have a signature, for example

onError:(Throwable) -> Unit = {} 

How can I distinguish this default value comes to the body or the value applied to this function?

+3
function android lambda kotlin
source share
1 answer

You cannot check if the lambda body is empty (therefore it does not contain the source code), but you can check if the lambda is your default value by creating a constant for this value and using this 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 } } 
+9
source share

All Articles