On 08/10/2018 09:34 AM, Niall Douglas wrote:
> Just to keep everybody on WG21 up to date, WG14 likes this proposal a lot, and apart from the .left, .right and .positive being replaced with _EitherValue(), _EitherError() and _EitherHasValue(), the above is essentially the paper to be published.
>
> Obviously if you have any additional feedback, please send it.
I understand you're aiming at absolute minimum to improve chances
of success, but I wonder whether there is any plan to later on add
an stdbool.h-like header that gives these new symbols prettier
non-_Uppercase (non-implementation-reserved) names, like:
#define fails _Fails
#define try _Try
#define catch _Catch
#define failure _Failure
#define either _Either
...
etc.
?
It feels like if the proposal succeeds, this new failure mechanism
will be used pervasively, and people will quickly get bored with
typing all that _Uppercasing for such a core feature, and
projects will grow such defines themselves.
With that, for mixed C and C++ codebases, and for ease of
codebase migration from C to C++, it'd be ideal if C code using
the prettified "try" and "catch" could compile with a C++
compiler. I'm not sure that would be possible. Seems worth
it to me to aim at making this path down the road possible.
I was a bit surprised to not see this discussed in the paper.
Your example in the paper would read instead:
#include <stdwhatever.h>
int some_function(int x) fails(float)
{
return (x != 0) ? 5 : failure(2.0);
}
const char *some_other_function(int x) fails(float)
{
int v = try(some_function(x));
return (v == 5) ? "Yes" : "No";
}
int main(int argc, char *argv[])
{
if(argc < 2)
abort();
either(const char *, float) v = catch(some_other_function(atoi(argv[1])));
if(v.positive)
{
printf("v is a successful %s\n", v.left);
}
else
{
printf("v is failure %f\n", v.right);
}
return 0;
}
Thanks,
Pedro Alves