Boost logo

Boost :

Subject: Re: [boost] Outcome v2
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2017-07-12 23:27:59


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<FILE *,EC1> foo() noexcept;
>
> outcome::result<int,EC2> 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<int,EC2> 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<int,EC2> 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.)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk