Skip to content

Commit eda5c43

Browse files
Improve/core extensions docs (#38)
## Summary Enhanced documentation for functional programming utilities in `dart_node_core/extensions.dart`. ## Changes - Added comprehensive library-level documentation explaining FP inspiration - Enhanced `NullableExtensions.match()` with detailed examples and use cases - Enhanced `ObjectExtensions.let()` with practical chaining examples - Included parameter descriptions and return value documentation - Added comparison examples showing imperative vs functional approaches ## Impact - Improves developer experience for core API utilities - Makes functional programming patterns more discoverable - Reduces learning curve with clear, practical examples - Zero breaking changes - pure documentation improvement The `match` and `let` utilities are powerful but were undocumented, creating friction for developers trying to understand when and how to use them. --------- Co-authored-by: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com>
1 parent 4009c3f commit eda5c43

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed
Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,73 @@
1-
/// Extension methods FP style transformations
1+
/// Functional programming extensions for nullable and non-null types.
2+
///
3+
/// Provides pattern matching and transformation utilities inspired by
4+
/// functional programming languages like Kotlin and Rust.
5+
library;
6+
7+
/// Extension methods for nullable values enabling pattern matching and
8+
/// functional transformations.
9+
///
10+
/// Example:
11+
/// ```dart
12+
/// String? getName() => 'World';
13+
///
14+
/// final greeting = getName().match(
15+
/// some: (name) => 'Hello, $name!',
16+
/// none: () => 'Hello, stranger!',
17+
/// );
18+
/// ```
219
extension NullableExtensions<T extends Object> on T? {
320
/// Pattern match on nullable value with cases for non-null and null.
21+
///
22+
/// This provides a safe way to handle nullable values by requiring
23+
/// explicit handling of both the present (`some`) and absent (`none`) cases.
24+
///
25+
/// **Parameters:**
26+
/// - `some`: Function called when this value is non-null
27+
/// - `none`: Function called when this value is null
28+
///
29+
/// **Returns:** The result of calling either `some` or `none`
30+
///
31+
/// Example:
32+
/// ```dart
33+
/// int? maybeNumber = 42;
34+
///
35+
/// final result = maybeNumber.match(
36+
/// some: (n) => 'Number: $n',
37+
/// none: () => 'No number',
38+
/// ); // Returns: "Number: 42"
39+
/// ```
440
R match<R>({required R Function(T) some, required R Function() none}) =>
541
switch (this) {
642
final T value => some(value),
743
null => none(),
844
};
945
}
1046

11-
/// Extension methods FP style transformations
47+
/// Extension methods for non-null values enabling functional transformations.
48+
///
49+
/// Provides utilities for applying transformations to values in a functional style.
1250
extension ObjectExtensions<T extends Object> on T {
13-
/// Apply function [op] to this value if non-null and return the result.
51+
/// Apply function [op] to this value and return the result.
52+
///
53+
/// This is useful for chaining operations and avoiding temporary variables.
54+
/// Also known as the "let" operation in Kotlin or "tap" in other languages.
55+
///
56+
/// **Parameters:**
57+
/// - `op`: Function that transforms this value into a result of type `R`
58+
///
59+
/// **Returns:** The result of calling `op` with this value
60+
///
61+
/// Example:
62+
/// ```dart
63+
/// final length = 'hello world'
64+
/// .let((s) => s.split(' '))
65+
/// .let((words) => words.length); // Returns: 2
66+
///
67+
/// // Instead of:
68+
/// final text = 'hello world';
69+
/// final words = text.split(' ');
70+
/// final length = words.length;
71+
/// ```
1472
R let<R>(R Function(T) op) => op(this);
1573
}

0 commit comments

Comments
 (0)