-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: Elvis Operator #25632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
A possible revision to this is to change the grammar to Either way, this would allow for the |
For Go simplicity sake, I think I'd prefer if gofmt allowed: func ErrorIfNil(arg interface{}) error {
if arg != nil { return ArgumentIsNilErr }
// instead of: arg ?: return ArgumentIsNilErr
return nil
} Your proposal saves lines, sure, but at the cost of new syntax. If on the other hand gofmt allowed the above, you save the same number of lines while reducing new syntaxes/etc. |
I think zig got it right with the
Essentially equivalent to
|
@isaachier I like that |
The zig It's the |
This is how I feel about these Usually just adding a filename, domain, exit code, timeout / duration gives |
OK I simplified the zig case. In that language, the error type is specified as part of the function return type ( |
I do not agree to put in new operators, but at least new operator's subject should be |
@zevdg luckily zig has built in stack traces (that look much better than Go's sadly cryptic stack traces IMO). |
No thank you, no thank you very much. |
@as if you don't have new information or technical argumentation to add, please express such opinions using the GitHub voting mechanism. See https://github.com/golang/go/wiki/NoMeToo. Thanks. |
A common idiom:
becomes:
I find the asymmetry there pretty strange here. To address that, you could suppose
I prefer the original Go code to either of these examples. |
Shouldn't this be "if its left is not a zero value"?
Also the "Argument Verification" example (test for == nil) does not match the semantics established in the "Error Handling" section (test for != nil). |
Why does nobody notice the obvious error in func ErrorIfNil(arg interface{}) error {
arg ?: return ArgumentIsNilErr
// equivalent: if arg != nil { return ArgumentIsNilErr }
return nil
} This is plain wrong, it must be func ErrorIfNotNil(arg interface{}) error {
arg ?: return ArgumentIsNotNilErr
// equivalent: if arg != nil { return ArgumentIsNotNilErr }
return nil
} Which clearly shows that it's not able to do argument verification (it is very rare that someone wants to verify that an argument should be |
Right. |
My proposal clearly has a lot of design flaws (ie I think #25626 is much more simple and readable than #21161, and it's pretty intuitive, so I'd highly recommend checking it out! |
Yet another solution for error handling TM
The Elvis Operator
?:
Seeing all of the solutions to error handling, here is what matters: if err isn't nil, what do we do?
So therefore I propose the following syntax, the "elvis operator":
?:
. It evaluates an expression on it's left, and if the left side is not nil, the return statement on the right is executed.The grammar would be
Expression "?:" ReturnStmt
. Note that because the right-hand side isReturnStmt
, the Elvis Operator is not a standard binary expression.Also, the syntax is inspired by Kotlin (although Kotlin's is a true binary operator), which is in turn inspired by C's ternary operator.
The decision to make the right side a ReturnStmt instead of an ExpressionList (which would reduce boilerplate code) is made because all exits of a function should have a return. This makes it less confusing for a reader and more clear as to what it does.
Error Handling
This is superior to #21161 because it has much less implicit behavior. The
return
is shown, anderr
is previously defined. It is also more flexible, and can be used in other cases...Argument Verification
Advantages
return
is explicit, so it's obvious where the function exits.err ?: return err
just look like "if there is an error, return it".The text was updated successfully, but these errors were encountered: