You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This also goes against the opinions of the go developers (https://go.dev/doc/faq#Does_Go_have_a_ternary_form), however much like in the proposal previously mentioned I think that this could make code cleaner and easier to read and have given 2 examples of this further down.
Proposal
What is the proposed change?
Add a way to return data in a switch statement to assign a variable or pass to a function argument
Who does this proposal help, and why?
It helps right slightly cleaner code for example (example syntax of this feature):
Is better then assigning an age var and ageDescription var, and then setting ageDescription in each case in the switch. This can become much worse if you are calling a function (example of what this would be like in old go):
someVar=56switchsomeVar {
case0..10:
someFunc("very very long argument A", "very very long argument B", "very very long argument C", "very very long argument D", "your value that changes to A")
case10..25:
someFunc("very very long argument A", "very very long argument B", "very very long argument C", "very very long argument D", "your value that changes to B")
case10..25:
someFunc("very very long argument A", "very very long argument B", "very very long argument C", "very very long argument D", "your value that changes to C")
}
Youcanquicklyseehowthisbecomeshardtomanage.
Please describe as precisely as possible the change to the language.
When the switch is used as a token and their is a token at the end of its value, then return that token and if their is no token then return an error when compiling.
What would change in the language spec?
Add a header on inline switches within the switch statements header that describes how they work, and in the code example add the switch statement example as seen above, or something similar.
Please also describe the change informally, as in a class teaching Go.
Switches can also return values like functions, to do this simple add a value at the end of each case statement to be returned.
Is this change backward compatible?
Yes.
Orthogonality: how does this change interact or overlap with existing features?
As I am still learning go I do not know many advanced futures, but from what I do know I don't think that it does interact with any.
Costs
Would this change make Go easier or harder to learn, and why?
If anything easier, because some code can become cleaner.
What is the cost of this proposal? (Every language change has a cost).
I can't think of any other then a little bit more complexity in the go compiler.
How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
gofmt may have to format this but I expect that it would be a simple change and no other tools are affected.
What is the compile time cost?
Any slowdown should not be perceivable compared to random error.
What is the run time cost?
I don't think that their is a runtime cost.
The text was updated successfully, but these errors were encountered:
Author background
I am a novice go programmer but know lots of other languages such as
Related proposals
Proposal
Add a way to return data in a switch statement to assign a variable or pass to a function argument
It helps right slightly cleaner code for example (example syntax of this feature):
Is better then assigning an
age
var andageDescription
var, and then settingageDescription
in each case in the switch. This can become much worse if you are calling a function (example of what this would be like in old go):When the switch is used as a token and their is a token at the end of its value, then return that token and if their is no token then return an error when compiling.
Add a header on inline switches within the switch statements header that describes how they work, and in the code example add the switch statement example as seen above, or something similar.
Switches can also return values like functions, to do this simple add a value at the end of each case statement to be returned.
Yes.
As I am still learning go I do not know many advanced futures, but from what I do know I don't think that it does interact with any.
Costs
If anything easier, because some code can become cleaner.
I can't think of any other then a little bit more complexity in the go compiler.
gofmt may have to format this but I expect that it would be a simple change and no other tools are affected.
Any slowdown should not be perceivable compared to random error.
I don't think that their is a runtime cost.
The text was updated successfully, but these errors were encountered: