Hacker News new | past | comments | ask | show | jobs | submit login

You can use a returned stucts in c to pass both an err code and a value

    typedef struct
    {
       int err;
       int value;
    } rtn_t;

    rtn_t rtn = foo();
    if(rtn.err < 0)
       printf( "err=%i", rtn.err);
    else
       printf( "value = %i\n", rtn.value);



There are certain widely accepted norms on how to handle errors in C. It is best to restrain ourselves to "prior art" so that your code is immediately comfortable to read and use for a seasoned C dev. To list a few: return codes, errno and longjump


Don't use setjmp/longjmp unless you can guarantee that you own all frames between one and the other (i.e. don't use it as a public error reporting API in a library, for example). They don't play well with C++, and really anything else that needs to unwind the stack. Those who write R extensions in C++ know how messy it can be.


The only widely accepted one in that list is return codes.


> a seasoned C dev. To list a few: return codes, errno and longjump

A seasoned dev that thinks errno and longjump are good idea's should have retired 25 years ago.


Yeah, I just tried to be on the more "permissive" side of things. Though per-library errno is being used (zeromq, libuv).


> both

But only one of them would make sense at a given moment, so why waste memory?


So that at any given moment, you know which one you have.


Your example doesn't tell me which I have. It's assuming that err = 0 means success, but that's an assumption not a contract of the type.

By comparison some something like https://github.com/oktal/result will tell you if it's a success or error without any magic error codes that actually mean success.

You could do this with a type bit + union in C, it's just more painful without templates


That requires 1 bit of information, and there is often a way of embedding that bit in the value itself; but the general technique of representing alternatives is called “a tagged union.” (You can also hear the fancy terms “the product type” for the struct and “the sum type” for the union.)


Accessing that one bit can be as expensive as passing an int around.


> so why waste memory?

In c it'll be either in a register or on the stack.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: