From 8ba959228d57a9752650fbac1754400c81d60ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81s=20Garci=CC=81a?= Date: Thu, 18 Sep 2025 00:56:45 -0500 Subject: [PATCH] feat(result): add getBoth method for Dart 3 record patterns Add a new method that returns a record with success and error values for destructuring. Include documentation and tests for the new functionality. --- README.md | 19 +++++++++++++++++++ lib/src/result_dart_base.dart | 19 +++++++++++++++++++ test/src/result_dart_base_test.dart | 18 ++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/README.md b/README.md index f408438..916900c 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` 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); + }); + }); }