Fix typos and improve clarity of or_return_operator

This commit is contained in:
gingerBill
2021-08-15 18:36:34 +01:00
parent ac08d37ca0
commit dc8cfcf92a
+14 -16
View File
@@ -2063,13 +2063,13 @@ or_return_operator :: proc() {
return err; return err;
} }
// The above idiom can be transformed into the follow // The above idiom can be transformed into the following
n1 := caller_2() or_return; n1 := caller_2() or_return;
// And if the expression has no other, it can be used like this // And if the expression is 1-valued, it can be used like this
caller_1() or_return; caller_1() or_return;
// which is functionally equivalen to // which is functionally equivalent to
if err1 := caller_1(); err1 != nil { if err1 := caller_1(); err1 != nil {
return err1; return err1;
} }
@@ -2090,30 +2090,28 @@ or_return_operator :: proc() {
return; return;
} }
// The above idiom can be transformed // The above idiom can be transformed into the following
y := caller_2() or_return; y := caller_2() or_return;
// And if the expression has no other, it can be used like this // And if the expression is 1-valued, it can be used like this
caller_1() or_return; caller_1() or_return;
// which is functionally equivalen to // which is functionally equivalent to
if err1 := caller_1(); err1 != nil { if err1 := caller_1(); err1 != nil {
err = err1; err = err1;
return; return;
} }
// If a the other values need to be set depending on what the end value is, // If a non-bare 'return' is required, then a normal 'if' can be used as it is
// the 'defer if' is can be used // a lot clearer to read
defer if err != nil { if z, zerr := caller_2(); zerr != nil {
n = -1; return -345 * z, zerr;
} }
// If a non-bare return is required, then a normal if is a lot clearer // If the other return values need to be set depending on what the end value is,
// and gets around the short circuiting // the 'defer if' idiom is can be used
if z, zerr := caller_2(); zerr != nil { defer if err != nil {
n = -z; n = -1;
err = zerr;
return;
} }
n = 123; n = 123;