nullkotlin

Kotlin Set to Null If Not Null


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

Solution

  • You can use the execute if not null idiom:

    test?.let { test = null }