Rene GensAuthorBlogGitHubLinkedIn

Idiomatic if with kotlin

07 March, 2018

Here is a safe and idiomatic way to the null safe Kotlin operator to check against a value and execute some code.

someObject?.takeIf{ it.status }?.apply{ doThis() }

And here is a real word example:

item?.takeIf { it.itemId == android.R.id.home }?.run {             
    onBackPressed()         
}

Thanks for reading! Check out more posts on the blog if you'd like.

© 2025, Built with Gatsby