From 8df1314e00f342ab059bf1fa8c600966e579dd93 Mon Sep 17 00:00:00 2001 From: Jesse Gottlieb <41432602+j-gottlieb@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:28:49 -0400 Subject: [PATCH] Add XXXIf and XXXUnless version of the appropriate scope functions --- libraries/stdlib/src/kotlin/util/Standard.kt | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/libraries/stdlib/src/kotlin/util/Standard.kt b/libraries/stdlib/src/kotlin/util/Standard.kt index 0aecfa3234fb0..490a06133a414 100644 --- a/libraries/stdlib/src/kotlin/util/Standard.kt +++ b/libraries/stdlib/src/kotlin/util/Standard.kt @@ -84,6 +84,22 @@ public inline fun T.apply(block: T.() -> Unit): T { return this } +/** + * If the predicate is true, calls the specified function [block] with `this` value as its receiver. Always returns `this` value. + * + * For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#apply). + */ +@kotlin.internal.InlineOnly +public inline fun T.applyIf(predicate: Boolean, block: T.() -> Unit): T = if (predicate) apply(block) else this + +/** + * Unless the predicate is true, calls the specified function [block] with `this` value as its receiver. Always returns `this` value. + * + * For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#apply). + */ +@kotlin.internal.InlineOnly +public inline fun T.applyUnless(predicate: Boolean, block: T.() -> Unit): T = if (!predicate) apply(block) else this + /** * Calls the specified function [block] with `this` value as its argument and returns `this` value. * @@ -99,6 +115,22 @@ public inline fun T.also(block: (T) -> Unit): T { return this } +/** + * If the predicate is true, calls the specified function [block] with `this` value as its receiver. Always returns `this` value. + * + * For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#apply). + */ +@kotlin.internal.InlineOnly +public inline fun T.alsoIf(predicate: Boolean, block: (T) -> Unit): T = if (predicate) also(block) else this + +/** + * Unless the predicate is true, calls the specified function [block] with `this` value as its receiver. Always returns `this` value. + * + * For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#apply). + */ +@kotlin.internal.InlineOnly +public inline fun T.alsoUnless(predicate: Boolean, block: (T) -> Unit): T = if (!predicate) also(block) else this + /** * Calls the specified function [block] with `this` value as its argument and returns its result. *