diff --git a/README.md b/README.md index f408438..5a540f3 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,25 @@ void main() { } ``` +#### Handling the Result with `getBoth` (from Dart 3) + +Returns a record containing both success and error values, +with one of them being null. +This allows for easy destructuring using Dart 3's record patterns. + +```dart +void main() { + final result = getSomethingPretty(); + final (:success, :error) = result.getBoth(); + + if (success != null) { + print('Operation succeeded with: $success'); + } else { + print('Operation failed with: $error'); + } +} +``` + ### Transforming a Result #### Mapping success value with `map` @@ -491,7 +510,7 @@ e 🚧 para o que estΓ‘ sendo trabalhado ----> ## Features - βœ… Result implementation. -- βœ… Result`s operators(map, flatMap, mapError, flatMapError, swap, when, fold, getOrNull, exceptionOrNull, isSuccess, isError). +- βœ… Result`s operators(map, flatMap, mapError, flatMapError, swap, when, fold, getOrNull, exceptionOrNull, isSuccess, isError, getBoth). - βœ… AsyncResult implementation. - βœ… AsyncResult`s operators(map, flatMap, mapError, flatMapError, swap, when, fold, getOrNull, exceptionOrNull, isSuccess, isError). - βœ… Auxiliar functions (id, identity, success, failure). diff --git a/lib/src/result_dart_base.dart b/lib/src/result_dart_base.dart index 8d89428..3728f5e 100644 --- a/lib/src/result_dart_base.dart +++ b/lib/src/result_dart_base.dart @@ -110,6 +110,15 @@ sealed class ResultDart { G Function(S success) onSuccess, W Function(F failure) onFailure, ); + + /// Returns a record containing both success and error values, + /// with one of them being null. + /// + /// This allows for easy destructuring using Dart 3's record patterns. + /// + /// Example: + /// final (:success, :error) = result.getBoth(); + ({S? success, F? error}) getBoth(); } /// Success Result. @@ -259,6 +268,11 @@ final class Success // (f) => Failure(failure), ); } + + @override + ({S? success, F? error}) getBoth() { + return (success: _success, error: null); + } } /// Error Result. @@ -405,4 +419,9 @@ final class Failure // (f) => Failure(failure), ); } + + @override + ({S? success, F? error}) getBoth() { + return (success: null, error: _failure); + } } diff --git a/test/src/result_dart_base_test.dart b/test/src/result_dart_base_test.dart index a279576..5a8931c 100644 --- a/test/src/result_dart_base_test.dart +++ b/test/src/result_dart_base_test.dart @@ -8,4 +8,22 @@ void main() { expect(result, isA>()); expect(result.getOrThrow(), 1); }); + group('getBoth', () { + test('should return a record with success value and null error on Success', + () { + final result = Success('success'); + final (:success, :error) = result.getBoth(); + expect(success, 'success'); + expect(error, isNull); + }); + + test('should return a record with null success and error value on Failure', + () { + final exception = Exception('failure'); + final result = Failure(exception); + final (:success, :error) = result.getBoth(); + expect(success, isNull); + expect(error, exception); + }); + }); }