Result is a header-only minimalistic library for safe, correct and complete error handling in C++
This library guarantees the following properties:
- When a function that can generate an error is called, there is a compile-time guarantee that the possible correct value cannot be retrieved without checking if there is an error.
- Errors shall not create side-effects, and shall preserve safety and determinism. This implies throwing exceptions are not allowed as mechanism. The library supports an easy integration for functions that can throw exceptions.
- Errors and correct results shall be mutually exclusive. This implies using union types and therefore C++ std::variant and std::visit. The type used to represent a result that might have an error is
Result<T>whereTis the type of the correct result. Additionally, it is available aGenericResult<T,E>whereTis the correct result type andEthe error type. Result<T>objects shall be monadic-composible. This implies that twoResult<T>should be safely directly composable without any need for manual error checking. The library adds a bind method to compose results. As it's a monad, a functor map method is also added to transform safely theResult<T>types without doing any manual error checking either. ForGenericError<T,E>there is both bind as well as bind_error as welll as map and map_error for each type. Note thatGenericError<T,E>has two monads, one for the valid error and another for the error.- Errors shall be chainable, allowing the creation of a stack of errors with different levels of abstraction. There is also a mechanism to chain errors without manually checking for them using chain_if_error.
- 100% covered by unit tests
For usage see unit tests.
We wrote unit tests so that they serve for a repository of examples as well as for functionality validation.
make releasemake release && make testmake release && make test && make coverageFirst add to CMakeLists.txt the following find_package entry:
find_package(result CONFIG REQUIRED)
Finally, add to CMakeLists.txt the library in the included directories:
include_directories(include
# Other include dirs go here
${result_INCLUDE_DIRS}
)
First follow the previous section for CMake Projects to get the CMakeLists.txt ready.
The next step is adding the dependency in package.xml:
<build>result</build>Clone the result repository in your workspace /src folder
cd $WORKSPACE/src && git clone https://github.com/cmrobotics/result.gitFinally, add the include in your C++ sources:
#include <result.hpp>