takeUnless

//(T) -> it 으로 접근가능.
inline fun <T> T.takeUnless(predicate: (T) -> Boolean)
: T? = if (!predicate(this)) this else null

//takeIf 와 반대의 개념.

val ari = person.takeUnless { it.name == "ari" }
println(ari) // Person(name = gold, age = 18)

val gold = person.takeUnless { it.name == "gold" }
println(gold) // null

val default = person.takeUnless { it.name = "ari" } ?: Person("ari", 24)
println(default) // Person(name=gold, age=18)

Last updated