On 13/07/2017 05:11, Emil Dotchevski wrote:
Allow me to clarify. Suppose I have a function foo which returns FILE * on
success, some EC1 on failure, and another function bar(), which calls foo
and returns an int on success, some EC2 on failure. I believe in terms of
Outcome this would be:
outcome::result foo() noexcept;
outcome::result bar() noexcept {
if( auto r=foo() ) {
//no error, use r.value(), produce the int result or return EC2(x).
} else {
return ______;
}
}
What do you recommend in place of _____?
FWIW, I believe the Outcome code would be:
outcome::result bar() noexcept {
auto r = foo();
if (r) {
//no error, use r.value(), produce the int result or return EC2(x).
} else {
return make_ec2_from_ec1(r.error());
}
}
Or if EC2 is the same type as or constructible from EC1:
outcome::result bar() noexcept {
OUTCOME_TRY(v, foo());
//no error, use v, produce the int result or return EC2(x).
}
The general recommendation (as Andrzej has already pointed out) is to
use the same error code type so that you don't need to do conversions
(which might be lossy), but if you insist on using alternate types then
you have no choice but to do an error domain conversion. (Or package it
in std::error_code, which lets you use multiple error domains.)