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 1/2] 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); + }); + }); } From 595e921373c00d77bcbaa898022813814797409c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81s=20Garci=CC=81a?= Date: Thu, 29 Jan 2026 00:36:55 -0500 Subject: [PATCH 2/2] refactor: rename `error` field to `failure` in `Result.getBoth()` return type and usage. --- README.md | 4 ++-- lib/src/result_dart_base.dart | 12 ++++++------ test/src/result_dart_base_test.dart | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5a540f3..59d3109 100644 --- a/README.md +++ b/README.md @@ -304,12 +304,12 @@ This allows for easy destructuring using Dart 3's record patterns. ```dart void main() { final result = getSomethingPretty(); - final (:success, :error) = result.getBoth(); + final (:success, :failure) = result.getBoth(); if (success != null) { print('Operation succeeded with: $success'); } else { - print('Operation failed with: $error'); + print('Operation failed with: $failure'); } } ``` diff --git a/lib/src/result_dart_base.dart b/lib/src/result_dart_base.dart index 3728f5e..2ba3e14 100644 --- a/lib/src/result_dart_base.dart +++ b/lib/src/result_dart_base.dart @@ -117,8 +117,8 @@ sealed class ResultDart { /// This allows for easy destructuring using Dart 3's record patterns. /// /// Example: - /// final (:success, :error) = result.getBoth(); - ({S? success, F? error}) getBoth(); + /// final (:success, :failure) = result.getBoth(); + ({S? success, F? failure}) getBoth(); } /// Success Result. @@ -270,8 +270,8 @@ final class Success // } @override - ({S? success, F? error}) getBoth() { - return (success: _success, error: null); + ({S? success, F? failure}) getBoth() { + return (success: _success, failure: null); } } @@ -421,7 +421,7 @@ final class Failure // } @override - ({S? success, F? error}) getBoth() { - return (success: null, error: _failure); + ({S? success, F? failure}) getBoth() { + return (success: null, failure: _failure); } } diff --git a/test/src/result_dart_base_test.dart b/test/src/result_dart_base_test.dart index 5a8931c..19b183d 100644 --- a/test/src/result_dart_base_test.dart +++ b/test/src/result_dart_base_test.dart @@ -12,18 +12,18 @@ void main() { test('should return a record with success value and null error on Success', () { final result = Success('success'); - final (:success, :error) = result.getBoth(); + final (:success, :failure) = result.getBoth(); expect(success, 'success'); - expect(error, isNull); + expect(failure, 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(); + final (:success, :failure) = result.getBoth(); expect(success, isNull); - expect(error, exception); + expect(failure, exception); }); }); }