add alsoIfTrue/alsoIfFalse/letIfTrue/letIfFalse

This commit is contained in:
InsanusMokrassar 2023-01-22 23:01:06 +06:00
parent 20799b9a3e
commit 8a6b4bb49e
1 changed files with 26 additions and 0 deletions

View File

@ -1,5 +1,31 @@
package dev.inmo.micro_utils.common
inline fun <T> Boolean.letIfTrue(block: () -> T): T? {
return if (this) {
block()
} else {
null
}
}
inline fun <T> Boolean.letIfFalse(block: () -> T): T? {
return if (this) {
null
} else {
block()
}
}
inline fun Boolean.alsoIfTrue(block: () -> Unit): Boolean {
letIfTrue(block)
return this
}
inline fun Boolean.alsoIfFalse(block: () -> Unit): Boolean {
letIfFalse(block)
return this
}
inline fun <T> Boolean.ifTrue(block: () -> T): T? {
return if (this) {
block()