{"action":"closed","issue":{"url":"https://api.github.com/repos/golang/go/issues/25273","repository_url":"https://api.github.com/repos/golang/go","labels_url":"https://api.github.com/repos/golang/go/issues/25273/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/25273/comments","events_url":"https://api.github.com/repos/golang/go/issues/25273/events","html_url":"https://github.com/golang/go/issues/25273","id":320610991,"number":25273,"title":"Proposal: add token to syntax to reduce error boilerplate","user":{"login":"mccolljr","id":8538024,"avatar_url":"https://avatars1.githubusercontent.com/u/8538024?v=4","gravatar_id":"","url":"https://api.github.com/users/mccolljr","html_url":"https://github.com/mccolljr","followers_url":"https://api.github.com/users/mccolljr/followers","following_url":"https://api.github.com/users/mccolljr/following{/other_user}","gists_url":"https://api.github.com/users/mccolljr/gists{/gist_id}","starred_url":"https://api.github.com/users/mccolljr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mccolljr/subscriptions","organizations_url":"https://api.github.com/users/mccolljr/orgs","repos_url":"https://api.github.com/users/mccolljr/repos","events_url":"https://api.github.com/users/mccolljr/events{/privacy}","received_events_url":"https://api.github.com/users/mccolljr/received_events","type":"User","site_admin":false},"labels":[{"id":236419512,"url":"https://api.github.com/repos/golang/go/labels/Proposal","name":"Proposal","color":"ededed","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/golang/go/milestones/30","html_url":"https://github.com/golang/go/milestone/30","labels_url":"https://api.github.com/repos/golang/go/milestones/30/labels","id":1373555,"number":30,"title":"Proposal","description":"Proposals that are pending (not yet accepted/rejected).\r\n","creator":{"login":"rsc","id":104030,"avatar_url":"https://avatars1.githubusercontent.com/u/104030?v=4","gravatar_id":"","url":"https://api.github.com/users/rsc","html_url":"https://github.com/rsc","followers_url":"https://api.github.com/users/rsc/followers","following_url":"https://api.github.com/users/rsc/following{/other_user}","gists_url":"https://api.github.com/users/rsc/gists{/gist_id}","starred_url":"https://api.github.com/users/rsc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsc/subscriptions","organizations_url":"https://api.github.com/users/rsc/orgs","repos_url":"https://api.github.com/users/rsc/repos","events_url":"https://api.github.com/users/rsc/events{/privacy}","received_events_url":"https://api.github.com/users/rsc/received_events","type":"User","site_admin":false},"open_issues":181,"closed_issues":494,"state":"open","created_at":"2015-10-24T01:40:49Z","updated_at":"2018-05-06T17:32:06Z","due_on":null,"closed_at":null},"comments":1,"created_at":"2018-05-06T17:26:39Z","updated_at":"2018-05-06T17:32:06Z","closed_at":"2018-05-06T17:32:06Z","author_association":"NONE","body":"### Proposal ###\r\nReduce error boilerplate by enabling the use of the `_!` token as syntactic sugar to cause a panic when assigned a non-nil `error` value\r\n\r\n```go\r\nval, err := something.MayError()\r\nif err != nil {\r\n    panic(err)\r\n}\r\n``` \r\ncould become\r\n\r\n```go\r\nval, _! := something.MayError()\r\n``` \r\nand\r\n\r\n```go\r\nif err := something.MayError(); err != nil {\r\n    panic(err)\r\n}\r\n``` \r\ncould become\r\n```go\r\n_! = something.MayError()\r\n``` \r\n\r\nOf course, the particular symbol is up for debate. I also considered `_^`, `_*`, `@`, and others. I chose `_!` as the de-facto suggestion because I thought it would be the most familiar at a glance.\r\n\r\nSyntactically, `_!` (or the chosen token) would be a symbol of type `error` available in the scope in which it is used. It starts out as `nil`, and any time it is assigned to, a `nil` check is performed. If it is set to a non-nil `error` value, a panic is started. Because `_!` (or, again, the token chosen) would not a syntactically valid identifier in go, name collision wouldn't be a concern. This ethereal variable would only be introduced in scopes where it is used, similar to named return values. If a syntactically valid identifier is needed, perhaps a placeholder could be used that would be re-written to a unique name at compile time.\r\n\r\n### Justification ###\r\nOne of the more common criticisms I see leveled at go is the verbosity of error handling. Errors at API boundaries aren't a bad thing. Having to get the errors to the API boundaries can be a pain though, especially for deeply recursive algorithms. To get around the added verbosity error propagation introduces to recursive code, panics can be used. I feel that this is a pretty commonly used technique. I've used it in my own code, and I've seen it used in the wild, including in go's parser. Sometimes, you've done validation elsewhere in your program and are expecting an error to be nil. If a non-nil error were to be received, this would violate your invariant. When an invariant is violated, it's acceptable to panic. In complex initialization code, sometimes it makes sense to turn errors into panics and recover them to be returned somewhere with more knowledge of the context. In all of these scenarios, there's an opportunity to reduce error boilerplate.\r\n\r\nI realize that it is go's philosophy to avoid panics as much as possible. They are not a tool for error propagation across API boundaries. However, they are a feature of the language and have legitimate use cases, such as those described above. Panics are a fantastic way to simplify error propagation in private code, and a simplification of the syntax would go a long way to make code cleaner and, arguably, clearer. I feel that it is easier to recognize `_!` (or `@`, or `_^, etc...) at a glance than the \"if-error-panic\" form. A token can dramatically decrease the amount of code that must be written/read to convey/understand:\r\n\r\n1.  there could be an error\r\n2. if there is an error, we're not expecting it\r\n3. if there is an error, it's probably being handled up the chain\r\n\r\nAs with any syntax feature, there's the potential for abuse. In this case, the go community already has a set of best practices for dealing with panics. Since this syntax addition is syntactic sugar for panic, that set of best practices can be applied to its use.\r\n\r\nIn addition to simplification of the acceptable use cases for panic, this also makes fast prototyping in go easier. If I have an idea I want to jot down in code, and just want errors to crash the program while I toy around, I could make use of this syntax addition rather than the \"if-error-panic\" form. If I can express myself in less lines in the early stages of development allows me to get my ideas into code faster. Once I have a complete idea in code, I go back and refactor my code to return errors at appropriate boundaries. I wouldn't leave free panics in production code, but they can be a powerful development tool.\r\n\r\n### Discussion ###\r\nI am open to any and all feedback from the go team and the community."}}
{"actor":{"display_login":"mccolljr"}}