dart_scope_functions
dart_scope_functions is a Dart utility library that implements Kotlin-inspired scope functions. These functions provide a convenient way to execute a block of code within the context of an object, making the code more readable and expressive.
Features
- Execute blocks of code within the context of an object.
- Chain multiple operations on an object in a readable manner.
- Conditional operations on objects, including nullable types.
Installation
Add dart_scope_functions to your pubspec.yaml file:
dependencies:
dart_scope_functions: latest
Then, run pub get to install the package.
Usage
Import the library:
import 'package:dart_scope_functions/dart_scope_functions.dart';
Example
void main() {
var result = 'Hello'.also((it) {
print(it); // Prints 'Hello'
}).let((it) {
return it.length;
});
print(result); // Prints 5
String? nullableString = null;
var defaultString = nullableString.withDefault('Default Value');
print(defaultString); // Prints 'Default Value'
var conditionResult = 42.takeIf((it) => it > 40);
print(conditionResult); // Prints 42
var runResult = run(() {
return 'Running a block';
});
print(runResult); // Prints 'Running a block'
}
API
Extensions on Any Type [T]
T also(void Function(T it) block)
Calls the specified function block with this value as its argument and returns this value.
block: A function to execute with the value. Must be synchronous: anasyncblock compiles, but itsFutureis discarded andalsoreturns before it completes.- Returns: The original value.
R let<R>(R Function(T it) block)
Calls the specified function block with this value as its argument and returns its result.
block: A function to execute with the value.- Returns: The result of
block.
Note: unlike Kotlin's
?.let,letdoes not short-circuit onnull. On a nullable receiver the block is invoked withit == null. Usevalue?.let(...)to skip the block, orletWithElseto supply a fallback.
T? takeIf(bool Function(T it) predicate)
Returns this value if it satisfies the given predicate or null if it doesn't.
predicate: A condition to evaluate.- Returns: The value if it satisfies the condition, otherwise
null.
T? takeUnless(bool Function(T it) predicate)
Returns this value if it does not satisfy the given predicate or null if it does.
predicate: A condition to evaluate.- Returns: The value if it does not satisfy the condition, otherwise
null.
Extensions on Nullable Type [T?]
R letWithElse<R>(R Function(T it) block, {required R Function() orElse})
Calls the specified function block with this value as its argument and returns its result. If this is null, it invokes orElse and returns its result instead. orElse is only evaluated when this is null.
block: A function to execute with the value if it's notnull.orElse: A function producing the fallback value; only called ifthisisnull.- Returns: The result of
block, or the result oforElse().
T withDefault(T defaultValue)
Returns this value if it's not null, otherwise returns the provided defaultValue. A method-chaining alternative to ??.
defaultValue: A default value to return ifthisisnull.- Returns: The value or the default value.
Global Functions
R run<R>(R Function() block)
Calls the specified function block and returns its result.
block: A function to execute.- Returns: The result of
block.
Libraries
- dart_scope_functions
- Kotlin-inspired scope functions implemented in Dart with the goal of executing a block of code within the context of an object.