The Ansi C20 standard is debating whether or not to include multiple returns. Why aren't these more common? They seemingly blow exceptions out of the water. Sample pseudo-syntax: (int, bool) unsafe_operation(int a, int b) { if (bad_thing_happens) { // (0, true) err_return true; } // (a+b, false) return a+b; }
int main() { int result; bool error; (result, error) = unsafe_operation(1,2); if (error) { // Data is invalid and should be ignored/handled } else { // Data is A-OK } return 0; }
This also has the benfit of being local and only causing execution to go up one level per error.
We already have Go with this shit and it's fucking retarded.
Asher Edwards
and namespasec please
Elijah Brooks
How is it worse than exceptions?
Adam Baker
>3.86 MB image png image of jpeg artifacts
John Bell
>new c standard fug nigga i ain usin dat shit dat c89 is ma nigga boi da fug u niggas tryna do
Juan Rodriguez
truly amazing
Alexander Wright
C11 is comfy though, you can do this shit struct OP { char* name; union { string postname; int postnumber; } }
struct OP test = {"Homo", .postnumber=1};
Isaiah Foster
yall niggas will get me to stop playn wit ma gang n ma homebois on irc befo u git me on dat shit
Mason Richardson
multiple returns are just a gay and restricted version of tuples. Whatever C is proposing won't look nearly as clean as what you posted because it conflicts with existing code (e.g. the comma operator)
Christopher Barnes
Just do this
int unsafe_operation(int *out) { if (bad_thing_happens()) return 0;
*out = ... return 1; } int main() { int result;
if (!unsafe_operation(&result)) return 1;
/* something with result */ return 0; }
Angel Powell
That's what I have been doing, it just doesn't seem "safe"
William Flores
Neither is what you're doing. If you want safety, beg for sum types to be added.
Single return error codes + out parameters is the best way because it allows you to conveniently write terse constructs like if (err = unsafe_op(&state)) // handle err
Hunter Sanchez
That's... Actually pretty good. Thanks, user.
Asher Nelson
if c programmers wanted an error handling system they would have moved on over to c++ by now its safe to say that the vast majority of c programmers dont want it, and if you add it in this standard then the adoption rate of this standard will be near-zero you struggled to get people to even consider adopting c11 and even then its only adopted because of managers with little technical background telling everyone to use it because he sees it is "new" and "modern" and therefore must be better
Alexander Thomas
>implying managers of any business use C for anything
Dylan Wilson
careful, you'll make the anti-go retards spam sh-- never mind, too late.
Eli Ortiz
It's not. He's sperging out. Fuck him.
Camden Green
Fucking ew. Please stop.
Jayden Bennett
struct better_than_c20 { int result; bool error; };
Anthony Evans
template struct BetterThanC89 { T val; E err; };
Chase White
they dont, which is my point they tell everyone else to, and what manager says - people do
Lincoln Anderson
Gross.
Bentley Cruz
>Why are exceptions more common than multiple returns? They're not alternatives to each other. Multiple returns have multiple applications. Consider some examples from CL: CL-USER> (floor 10 4) ; where 4 is the divisor 2 2
floor() returns two values, the quotinent and the remainder. CL-USER> (parse-integer " 234 56" :junk-allowed t) 234 5
parse-integer returns the parsed integer, and the index of the last used character in the string. The index can be used for splitting the string for furher parsing. CL-USER> (defvar table (make-hash-table)) TABLE CL-USER> (setf (gethash 1 table) 10) 10 CL-USER> (setf (gethash 2 table) nil) NIL CL-USER> (gethash 1 table) 10 T CL-USER> (gethash 2 table) NIL T CL-USER> (gethash 3 table) NIL NIL
gethash() returns two values, one for the value stored in the table and another signaling if the hash actually exists in the table. This way you can save a NIL into a hash table, and know that the NIL returned is intentional. CL-USER> (encode-universal-time 59 59 23 28 12 2010) 3502565999 CL-USER> (decode-universal-time *) 59 59 23 28 12 2010 1 NIL -1
This could probably be handled better but here's decode-universal-time() returning nine values because why not.