-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: Go 2: onErr
simple macro for common case error handling. A #32437 counter-proposal.
#32946
Comments
I think that in stead of implementing a specific built in "macro" such as |
As you've noted, this is short-hand for I feel like this adds extra cognitive overload, without really delivering value to developers (my 2c!).
I'm a big -1 on this idea - it will lead to everyones codebase being 'specialised', with custom macros and definitions all over the place. Switching between projects because a huge mental burden, and it starts to smell of similar features in other, older languages that quickly grow out of control. |
Note that I am in the 'leave |
The proposal says that if r, err := fn(); onErr && triesleft > 0 {
triesleft--
continue retry
} |
Simply replacing The fact that this adds special language treatment to the identifier -- writing for @golang/proposal-review |
This is a counter-proposal to the #32437. It uses the
on err
words proposed in #32611and #32848 but unlike #32611 it is specifically restricted to the ubiquitous and a defacto standarderr
identifier (variable) followed by a block, control-flow statement; or a call to the built-inpanic()
function.Usage examples
Go Specification amendments
Operators
Statements addition:
Blocks addition:
onErr macro
to be added after Defer_statements.
The
onErr
macro simplifies repeatable error handling, checking whethererr
variable is not nil. Ie. the veryonErr
always is expanded to meanif err != nil
. Theerr
identifier must be in the scope.After an
onErr
either ablock
, or five possible statements are allowed:continue
,break
,return
,goto
, and a call topanic(…)
built-in. Then expansion occurs:onErr
always is expanded to theif err != nil
statement.or
orand
operator after theonErr
, expansion ends.Implementation:
after recognizing an
onErr
token, hardcode theif err != nil
template and subparse rest of the line. The core of the expansion likely will land somewhere within parser.go:2040 scope in thecase _Onerr:
A macro?
@griesemer suggested that
try
is just a macro. So I dare to propose a macro too, based on an 'invisible semicolons' precedence and with a straightforward implementation.thats all,
enjoy!
ohir
Edits:
log_op
to allow expansion in expression chains. Add usage example for it.The text was updated successfully, but these errors were encountered: