Transform std::expected into error or value #1433
Answered
by
maikel
mathisloge
asked this question in
Q&A
-
Is it somehow possible to transform a result of type std::expected into either an error state or a valid value? stdexec::schedule(scheduler) | stdexec::then([](){
std::expected<int, MyError> my_operation = do_some_work();
if(my_operation.has_value()) { return *my_operation; };
// now set an error for MyError
})
| stdexec::upon_error([](MyError error) { // handleError })
| stdexec::then([](int value){});
...
Hope you know what I'm trying to do. |
Beta Was this translation helpful? Give feedback.
Answered by
maikel
Nov 9, 2024
Replies: 1 comment 3 replies
-
In principle that is doable but not intrinsically with then. You can define your own adaptor (which is more optimized) either by hand or by using let_value in conjunction with variant_sender, just, and just_error (which might be easier to do). |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The implementation in stdexec optimizes for various things.
I made this example not very long ago for a naive implementation of the then sender, maybe that helps you. https://godbolt.org/z/5qczb9dhh
I will try to make a tutorial on sender adaptors soonish, but maybe someone else has already done so.