Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/daquexian/coroutine_error_handling
https://github.com/daquexian/coroutine_error_handling
Last synced: 16 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/daquexian/coroutine_error_handling
- Owner: daquexian
- Created: 2020-03-23T03:18:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-03-23T03:19:29.000Z (over 4 years ago)
- Last Synced: 2024-10-07T09:02:49.778Z (about 1 month ago)
- Language: C++
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Coroutine as Error Handling
Either monad by coroutine introduced in C++20 based on [tl::expected](https://github.com/TartanLlama/expected) and [coroutine_monad](https://github.com/toby-allsopp/coroutine_monad)
Example:
```cpp
expected dividem(float a, float b) {
if (b == 0) {
return make_unexpected("Dividend in dividem is 0");
}
return a / b;
}expected sqrtm(float a) {
if (a < 0) {
return make_unexpected("Radicand in sqrtm < 0");
}
return sqrt(a);
}expected sqrt_of_quotient(float a, float b) {
float c = trym dividem(a, b);
float d = trym sqrtm(c);
retm d;
}int main() {
std::cout << sqrt_of_quotient(32, 2) << std::endl; // Result: 4
std::cout << sqrt_of_quotient(10, -1) << std::endl; // Error: Radicand in sqrtm < 0
std::cout << sqrt_of_quotient(-8, -2) << std::endl; // Error: Dividend in dividem is 0
return 0;
}
```Reference:
https://github.com/toby-allsopp/coroutine_monad
https://github.com/TartanLlama/expected