Using Kotlin takeIf (or takeUnless)

Nov 28, 2017 · 3 min read

In Kotlin’s standard functions, there’s two function i.e. takeIf and takeUnless, that at first glance, what’s so special about it? Is it pretty much just if?

Or one could go the extreme, replace every if it sees as below (NOT recommended).

// Original Code
if (status) { doThis() }
// Modified Code
takeIf { status }?.apply { doThis() }