-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Use an array as a ExpressionList for switch case statement #58868
Comments
Go 1.21 will have |
We already support |
Hi @ianlancetaylor |
@carlmjohnson func belongs[ T comparable ](item T, ar []T) bool {
for _, v := range ar {
if v == item {
return true
}
}
return false
} It is much shorter and doesn't require jumping to import section to add a package name. |
If you like your name better you can do
Have a look at https://pkg.go.dev/golang.org/x/tools/cmd/goimports |
@fzipp yeah, usually there is more than one way to solve an issue. it's why we love Go. The only feature that I miss there is a replacing unused variables with underscore. But switching this error to a warning at least on stage of development would be way better. But it's not the case of this proposal. I'm sure it's already proposed by other guys. |
So I meant it's not about function names. it's for a DRY principle. Any implementation will be ok for me. |
With the current language design, the compiler knows all values and creates appropriate optimizations. With your proposal, the compiler doesn't know almost anything anymore. How would you like your "match test"? Linear, bisecting, ...? Personally, I don't see how this convenience proposal improves Go without at in its current state having a lot of open questions. Especially performance-wise, because it is totally unclear how to deal with the arrays/slices in a general, yet performing way. I can imagine that this might have been the reasons for downvotes, but that's just my guess. |
@thediveo I don't see how it can break existent optimizations. I didin't check a golang code generator, but actually there are two ways to handle 'case'-statement with multiple expressions: One is an expanding one 'case'-statement with multiple expressions to several cases with single expression and convert in conditional jump of ASM code. Second is an evaluating list of expressions to an internal array of values and make seek of a matching value. |
This amounts to another way to do something that the language can already do. The benefits of this new feature don't outweigh the significant cost of changing the language. Therefore this is a likely decline. Leaving open for three weeks for final comments. |
@ianlancetaylor It can be closed. Anyway it's intended to be a kind of syntax sugar. thanks... |
it would be good to have an able to "unpack" array for switch/case statement in the same way as it can be for variadic function parameters. Example:
Or maybe make another builtin function (kinda
anyof
orin
in the same way asrange
) that could be used also with if statement.The text was updated successfully, but these errors were encountered: