Hello friends, I'm looking to hone in my coding style for pure C functions which can fail or return a value. I think traditionally a lot of libaries return error codes and treat one of the parameters as an output variable via a pointer. Although, I saw zig has an error union type. Basically automatically create a structure which appends an error code to the struct, I was considering using macros to emulate this functionality, what are your thoughts?
// Traditional
error make_context( context_struct * out_context, const input_params* const in_params );
int main() {
error err = make_context(&ctx, &in_params);
if(err != success) return err;
ctx.do_something();
}
// Error Union
MAKE_ERROR_OR_STRUCT(context_struct)
context_struct_or_error make_context( const input_params* const in_params );
int main() {
context_struct_or_error ctx = make_context(&in_params);
if(ctx.err != success) return err;
ctx.value.do_something();
}