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

Updated Standard.kt with performOperation function #5294

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions libraries/stdlib/src/kotlin/util/Standard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,27 @@ public inline fun repeat(times: Int, action: (Int) -> Unit) {
action(index)
}
}

/**
* This function performs a given operation on an input of type T.
* The operation is specified by a string and can be one of the following: "run", "also", "with", "apply".
* The operation is performed by calling the corresponding scope function in Kotlin.
* The function takes a lambda with receiver as a parameter, which is the block of code to be executed within the scope function.
* If an unsupported operation is provided, the function throws an IllegalArgumentException.
*
* @param input The input on which the operation is performed.
* @param operation The operation to be performed. Can be one of the following: "run", "also", "with", "apply".
* @param block The block of code to be executed within the scope function.
* @return The result of the operation. The type of the result depends on the operation and the block of code.
* @throws IllegalArgumentException If an unsupported operation is provided.
*/
@kotlin.internal.InlineOnly
public fun <T> performOperation(input: T, operation: String, block: T.() -> Unit): Any? {
return when (operation) {
"run" -> input.run(block)
"also" -> input.also(block)
"with" -> with(input, block)
"apply" -> input.apply(block)
else -> throw IllegalArgumentException("Operation not supported")
}
}