-
Notifications
You must be signed in to change notification settings - Fork 18k
spec: add "with" conditional branch #68968
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
How does "always" differ from "else true"? |
I don't understand the example. It boils down to if c1 {
...
} else {
if c2 {
...
}
f()
} This converts to if c1 {
...
} else if c2 {
...
} always {
f()
} For the converted code to be the same as the original code, it must be the case that the |
I believe that the intent is to add a new keyword |
I'm also finding the OP proposal confusing. It breaks the identation logic as can be seen in the example. It is unexpected in that it looks much like a Python |
the proposal is for a new keyword, but one that is only valid in the context of an if / else block and not as a "condition" of "if" or "else if"... thus it doesn't conflict with variables named "always". @ianlancetaylor the example does not reduce to that simplified conditional because conditions in go can be used to declare and assign variables within the scope of the if/else block.
creates variable "a" that is valid for the scope of the conditional block. in this...
fn3(a) is invalid because the scope of a has already disappeared. the ability to declare variables for only the scope of a sequence of if/else conditionals leads to clean, sequential, maintainable, readable code until the programmer needs to perform some work on or with a. the proposal is not for a "finally" but for an "always" such that
would also be valid. this allows us to "do some work" with "a" (which entered the scope through a conditional assignment) before proceeding with the subsequent branches of the conditional and without introducing a nested conditional scope in an "else" block. |
fwiw, i'm not married to "always". "next" or "do". "and" makes sense, but conflates with the boolean operator. perhaps "with", which is pythonic i suppose. |
@jkassis Thanks, but that doesn't answer my question. My question is: when does the Your original post says that this if a, err := f(b); err != nil {
...
} else {
if b, ok := bar(a); ok {
...
}
a.foo()
} is equivalent to this: if a, err := f(b); err != nil {
...
} else if b, ok := bar(a); ok {
...
} always {
a.foo()
} The first code sequence calls If the second code sequence is equivalent, then the So when precisely is the |
@ianlancetaylor ... oh my goodness. i see your point and i did make a mistake in my example. please forgive. this example is to the point...
would be...
|
noting that i changed the "always" keyword to "with" as per the evolving conversation. |
Can there be multiple "with" keywords? Or does it depend on where it is located? |
definitely, though a |
here's a practical example...
becomes...
|
obviously, you can also put more than one statement in the with clause, which is an extra advantage. |
You can put more than one statement in an else if clause too, sort of...
|
that's true, but less readable imo. |
This is syntactic sugar for a nested |
How much emoji is required?
As a former Google Borg-man, I'm sure others would feel the same. |
@rebeccajae can you give me some ❤️ |
@adamrogoyski can you give me some ❤️ too? |
There is no specific requirement, but this proposal is all thumbs-down and no thumbs-up. By way of comparison, in Go 1.21 we had a fairly minor change to the language: adding a |
wow. so many thumbs down. i suspect because the original example was in error. i updated the proposal title and content, but suspect i won't get the votes. can you please point me to the relevant package. i'll provide the PR. |
was looking through the "clear" proposal... how would i locate the PR for code associated with that? |
@jkassis This is getting off-topic, but you can search for the gopherbot comments saying "Change https://go.dev/cl/... mentions this issue". In this case the main change is: https://go-review.googlesource.com/c/go/+/453395 |
This proposal is syntactic sugar, so several places need changing. The compiler parser in cmd/compile/internal/syntax. The internal IR may need changing in cmd/compile/internal/ir, I'm not sure. Something will need to translate the new syntax into existing constructs somewhere. Then there is separate code that needs changing in go/parser, go/ast, go/print, and probably go/types. Note that writing a pull request for this proposal is not going to make us any more likely to accept it. We are currently path to declining this proposal. |
No change in consensus. |
Go Programming Experience
Experienced
Other Languages Experience
So many
Related Idea
Has this idea, or one like it, been proposed before?
I don't think so.
Does this affect error handling?
No.
Is this about generics?
No.
Proposal
create an "with" branch for conditionals. the scope of with includes the scope of "if-else" so that variables declared and assigned in the scope of "if-else" appear in the scope of "with" and variable declared in the scope of "with" appear in the scope of the the "if-else". in some cases, this removes the need for a nested "if-else" block.
this...
would be...
for complex blocks...\
else if func () bool {... ; return false }()
becomes
with{...}
Language Spec Changes
addition of an "always" block to if-else sequences.
Informal Change
always blocks allow you to perform operations on variables declared and assigned in if-else statements.
Is this change backward compatible?
yes.
Orthogonality: How does this change interact or overlap with existing features?
no.
Would this change make Go easier or harder to learn, and why?
easier. cleaner syntax. less nesting is always good.
Cost Description
trival.
Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: