Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add applyIf, applyUnless, alsoIf, alsoUnless scope functions #5373

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions libraries/stdlib/src/kotlin/util/Standard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ public inline fun <T> 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> 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> 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.
*
Expand All @@ -99,6 +115,22 @@ public inline fun <T> 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).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy-and-paste

*/
@kotlin.internal.InlineOnly
public inline fun <T> 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> 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.
*
Expand Down