-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add monadic functions to
Result
(#3957)
In order to make the `Result` type easier to use, this commit adds three new functions: * `Result::value_or` allows the user to obtain the value or a provided default value. * `Result::transform` models functorial mapping, allowing users to modify values inside results. * `Result::and_then` models monadic binding, allowing users to build complex chains of actions on results. Implemented are lvalue and rvalue versions of these functions as well as tests. ![image](https://github.com/user-attachments/assets/591e64a2-e4fb-4ce6-9d34-166b28f7a837) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced `Result` class with new methods for improved error handling and value transformation: - `value_or`: Retrieve valid value or default. - `transform`: Apply a function to the valid value. - `and_then`: Chain functions based on the result's validity. - **Tests** - Added comprehensive test cases to validate the new functionalities of the `Result` class, ensuring reliability and correctness: - `ValueOrResult`: Validates the `value_or` method. - `TransformResult`: Assesses the `transform` method. - `AndThenResult`: Evaluates the `and_then` method. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
1 parent
f16ed67
commit 9067679
Showing
2 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters