-
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: spec: extend comma-ok expressions to slices, array and string #54192
Comments
v, ok := a[i]
syntax
See also #6815 and #30209 for with generics you could write an index function, perhaps with a user provided default value which may be more useful Please fill out https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing language changes |
sure, I will follow the proper proposal process later, just want to know people's reactions first 😄 |
For maps, |
Yes, the guidelines for comma-ok forms is what @randall77 describes: the comma-ok form only exists if it is difficult or impossible to determine the result in some other way. |
Based on the discussion above, this is a likely decline. Leaving open for four weeks for final comments. |
Is it otherwise possible to write generic functions that abstract over maps, arrays, and slices? If so, then it would be unfortunate to have to pass in a |
Currently you can't write a generic function that works for maps, arrays, and slices. We have no plans to support that in the future. Even if we did, it's not clear that it would make sense to use a comma-ok form, even if we made it syntactically valid. |
No change in consensus. |
according to https://go.dev/ref/spec#Index_expressions
I think it is nice to have this syntax for slice, array, or string.
we can return zero-value + false when the index is out of bound
wdyt?
EDIT:
the behavior should be as close to map type as possible, so, it will also work on nil slice
The text was updated successfully, but these errors were encountered: