Skip to content
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

Closed
win-t opened this issue Aug 2, 2022 · 8 comments
Closed
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal Proposal-FinalCommentPeriod v2 An incompatible library change
Milestone

Comments

@win-t
Copy link

win-t commented Aug 2, 2022

according to https://go.dev/ref/spec#Index_expressions

An index expression on a map a of type map[K]V used in an assignment or initialization of the special form

v, ok = a[x]
v, ok := a[x]
var v, ok = a[x]

yields an additional untyped boolean value. The value of ok is true if the key x is present in the map, and false otherwise.

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

@win-t win-t added the Proposal label Aug 2, 2022
@gopherbot gopherbot added this to the Proposal milestone Aug 2, 2022
@seankhliao seankhliao changed the title proposal: allowing other type than map to use v, ok := a[i] syntax proposal: Go 2: spec: extend comma-ok expressions to slices Aug 2, 2022
@seankhliao seankhliao added LanguageChange Suggested changes to the Go language v2 An incompatible library change labels Aug 2, 2022
@seankhliao
Copy link
Member

See also #6815 and #30209 for _, ok := for ints

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

@win-t
Copy link
Author

win-t commented Aug 2, 2022

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 😄

@win-t win-t changed the title proposal: Go 2: spec: extend comma-ok expressions to slices proposal: Go 2: spec: extend comma-ok expressions to slices, array and string Aug 2, 2022
@randall77
Copy link
Contributor

For maps, x, ok := m[k] is necessary because there's no other way to know if k is in the map.
For slices, it is easy to compute: ok := i >= 0 & i < len(s). So while I'm sure it would be convenient at times to have a shorter form, it isn't necessary.

@ianlancetaylor
Copy link
Contributor

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.

@ianlancetaylor
Copy link
Contributor

Based on the discussion above, this is a likely decline. Leaving open for four weeks for final comments.

@bcmills
Copy link
Contributor

bcmills commented Oct 6, 2022

the comma-ok form only exists if it is difficult or impossible to determine the result in some other way.

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 func(c Container, k Key) (ok bool) just to smooth out the difference between the two key-checking idioms.

@ianlancetaylor
Copy link
Contributor

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.

@ianlancetaylor
Copy link
Contributor

No change in consensus.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2022
@golang golang locked and limited conversation to collaborators Nov 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal Proposal-FinalCommentPeriod v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants