proposal: spec: finer control of variable scope in if (& other) statements #70337
Closed
1 of 4 tasks
Labels
LanguageChange
Suggested changes to the Go language
LanguageChangeReview
Discussed by language change review committee
Proposal
Milestone
Go Programming Experience
Intermediate
Other Languages Experience
No response
Related Idea
Has this idea, or one like it, been proposed before?
No, not that I could find (but tricky to search for).
Does this affect error handling?
No.
Is this about generics?
No.
Proposal
Secondary return values to indicate boolean success or errors are common and idiomatic in Go, in fact much of Go's own standard library uses it. This often leads to code such as
or an alternate form
Both versions have their own upsides and downsides: the former keeps "ok" scoped as narrowly as possible, and leaking variable scope is (anecdotally) a somewhat common source of bugs. On the other hand, the latter reduces mental complexity by avoiding "unnecessary" indentation through the early return, which is commonly a preferred style.
I'd like to propose that the language allows "exporting" (for a lack of better word) a variable defined within the narrower scope to the outer scope, in the above example:
Please note that I'm not proposing
$v
to be the syntax, it is merely a placeholder. I don't have a good suggestion for what the syntax should be. In any case, to clarify intent, the above would be expected to behave as if it had beenDisclaimer: This came up internally within our team. I do not think this proposal will reach consensus or even gain notable support, and there are certainly arguments against it such as increased learning cost for relatively minor benefit, or that this could indicate code smell (though I personally disagree on that, but it is hard to argue outside of real-world code bases). However, gauging reactions never hurts.
Language Spec Changes
No response
Informal Change
No response
Is this change backward compatible?
Yes, as it would be an opt-in through new syntax.
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
Learning Go from scratch would probably not become notably more difficult as this is an opt-in; however, understanding existing code bases arguably has an additional cost (as every addition to the language does). I think the actual cost here will depend on the syntax, and I don't think it hurts general readability, as the code can still be understood naively, and arguably novice Gophers are already likely to not understand the nuances of variable scope in these situations.
Cost Description
No response
Changes to Go ToolChain
No response
Performance Costs
Minimal compile time cost, no runtime cost.
Prototype
No response
The text was updated successfully, but these errors were encountered: