-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathfunctional_extender.dart
More file actions
40 lines (35 loc) · 1.04 KB
/
functional_extender.dart
File metadata and controls
40 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
extension FunctionalExtender<T> on T? {
/// ```dart
/// final String? myNullableVar = ...
///
/// // Really annoying repetitive condition
/// if (myNullableVar != null) return null;
///
/// return doSomethingElseWith(myNullableVar);
/// ```
///
/// This extension allow an alternative usage:
/// ```
/// final String? myNullableVar = ...
///
/// return myNullableVar?.apply((m) => doSomethingElseWith(m));
/// ```
R? apply<R>(R Function(T) f) {
// Local variable to allow automatic type promotion. Also see:
// <https://github.com/dart-lang/language/issues/1397>
final T? self = this;
return self == null ? null : f(self);
}
T? takeIf(bool Function(T) f) {
final T? self = this;
if (self == null) return null;
if (f(self)) {
return self;
} else {
return null;
}
}
}
const Deprecated willbemovedsoon = Deprecated(
'This method will be moved to another package in a next release.\nBe aware this method will not be removed but moved to another module outside of [saf].',
);