@@ -24,13 +24,16 @@ abstract class Result<S, E> {
2424 /// ```
2525 ///
2626 /// before casting the value;
27+ @Deprecated ('Will be removed in the next version. '
28+ 'Use `tryGetSuccess` or `tryGetError` instead.'
29+ 'You may also use `onSuccess` and on `onError` for similar result.' )
2730 dynamic get ();
2831
29- /// Returns the value of [S] .
30- S ? getSuccess ();
32+ /// Returns the value of [S] if any .
33+ S ? tryGetSuccess ();
3134
32- /// Returns the value of [E] .
33- E ? getError ();
35+ /// Returns the value of [E] if any .
36+ E ? tryGetError ();
3437
3538 /// Returns true if the current result is an [Error] .
3639 bool isError ();
@@ -47,6 +50,16 @@ abstract class Result<S, E> {
4750 W Function (S success) whenSuccess,
4851 W Function (E error) whenError,
4952 );
53+
54+ /// Execute [whenSuccess] if the [Result] is a success.
55+ R ? onSuccess <R >(
56+ R Function (S success) whenSuccess,
57+ );
58+
59+ /// Execute [whenError] if the [Result] is an error.
60+ R ? onError <R >(
61+ R Function (E error) whenError,
62+ );
5063}
5164
5265/// Success Result.
@@ -78,7 +91,8 @@ class Success<S, E> implements Result<S, E> {
7891 int get hashCode => _success.hashCode;
7992
8093 @override
81- bool operator == (Object other) => other is Success && other._success == _success;
94+ bool operator == (Object other) =>
95+ other is Success && other._success == _success;
8296
8397 @override
8498 W when < W > (
@@ -89,10 +103,18 @@ class Success<S, E> implements Result<S, E> {
89103 }
90104
91105 @override
92- E ? getError () => null ;
106+ E ? tryGetError () => null ;
93107
94108 @override
95- S ? getSuccess () => _success;
109+ S tryGetSuccess () => _success;
110+
111+ @override
112+ R ? onError <R >(R Function (E error) whenError) => null ;
113+
114+ @override
115+ R onSuccess <R >(R Function (S success) whenSuccess) {
116+ return whenSuccess (_success);
117+ }
96118}
97119
98120/// Error Result.
@@ -133,25 +155,34 @@ class Error<S, E> implements Result<S, E> {
133155 }
134156
135157 @override
136- E ? getError () => _error;
158+ E tryGetError () => _error;
159+
160+ @override
161+ S ? tryGetSuccess () => null ;
162+
163+ @override
164+ R onError <R >(R Function (E error) whenError) => whenError (_error);
137165
138166 @override
139- S ? getSuccess () => null ;
167+ R ? onSuccess <R >(R Function (S success) whenSuccess) => null ;
168+
140169}
141170
142171/// Default success class.
143172///
144173/// Instead of returning void, as
145174/// ```dart
146- /// Result<Exception, void >
175+ /// Result<void, Exception >
147176/// ```
148177/// return
149178/// ```dart
150- /// Result<Exception, SuccessResult >
179+ /// Result<SuccessResult, Exception >
151180/// ```
152181class SuccessResult {
153182 const SuccessResult ._internal ();
154183}
155184
156185/// Default success case.
157186const success = SuccessResult ._internal ();
187+
188+
0 commit comments