ScopeFunctionExtensions<T> extension

Extensions that can be used on any type T by using kotlin inspired scope functions.

on
  • T

Methods

also(void block(T it)) → T
Calls the specified function block with this value as its argument and returns this value. also Used for additional actions that don't alter the object, such as logging or printing debug information. something.also((it) { ... }); // returns something.
let<R>(R block(T it)) → R
Calls the specified function block with this value as its argument and returns its result. Often used for executing a code block only with non-null values.
run(T block()) → T
Calls the specified function block and returns its result. Used for evaluating a block of several statements where an expression is required. someNullable ?? run(() {
takeIf(bool predicate(T it)) → T?
Returns this value if it satisfies the given predicate or null if it doesn't. Used for checking some condition and returning the object itself if it matches the condition. someNullable?.takeIf((it) => it > 5); // returns someNullable if it is greater than 5.
takeUnless(bool predicate(T it)) → T?
Returns this value if it does not satisfy the given predicate or null if it does. Used for checking some condition and returning the object itself if it doesn't match the condition. someNullable?.takeUnless((it) => it > 5); // returns someNullable if it is not greater than 5.