Is there an idiom in Kotlin for setting a variable to null if it is not already null? Something more semantically pleasing than:
var test: String? = null
if(test != null) test = null
Just assign null to local variable:
test = null
In case if it's not null - you assign null to this variable. In case if variable is null - you just assign null to it, so nothing changed.
I came up with this extensions which makes this simpler:
inline fun <T, R> T.letThenNull(block: (T) -> R): T? { block(this); return null }
val test: Any? = null
...
test = test?.letThenNull { /* do something with test */ }