I have written a
whenNotNull
function:
inline fun <T : Any, R> whenNotNull(input: T?, callback: (T) -> R): R? = input?.let(callback)
usage:
override fun onReceive(context: Context?, intent: Intent?) {
whenNotNull(intent) {
it.someAction()
}
}
I have often problems with
shadowed variable names, e.g. I cannot write:
override fun onReceive(context: Context?, intent: Intent?) {
whenNotNull(intent) { intent -> // shadowed
intent.someAction()
}
}
Is there some construct to recognize that
intent
is not null within the
callback
block, so I can write:
override fun onReceive(context: Context?, intent: Intent?) {
whenNotNull(intent) {
intent.someAction()
}
}
I have enabled some static analyzer tools which complain about the "it". I know I can just write
whenNotNull(intent) { _intent -> }
but I'm curious if there is some Kotlin construct which targets this case.