Skip to content

proposal: maps: add Any function #67717

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

Closed
YairHalberstadt opened this issue May 30, 2024 · 3 comments
Closed

proposal: maps: add Any function #67717

YairHalberstadt opened this issue May 30, 2024 · 3 comments
Labels
Milestone

Comments

@YairHalberstadt
Copy link

YairHalberstadt commented May 30, 2024

Proposal Details

It's fairly common to want to get a key or value from a map, but where you don't care which one. For example, if all the entries in the map have the same value for one property.

This leads to a fairly common pattern:

var someKey
for key := range myMap {
   someKey = key
   break
}

whilst not very onerous, it breaks the flow, and it's common enough that I think it may be worth adding to the exp maps package:

// Any returns a non-deterministically chosen key and value from the map m
func Any[M ~map[K]V, K comparable, V any](m M) (K, V)
@gopherbot gopherbot added this to the Proposal milestone May 30, 2024
@mateusz834
Copy link
Member

Why x/exp? maps package is now part of std.

Also see: #66361

@YairHalberstadt
Copy link
Author

I didn't realise, thanks, std then.

Some of the proposals in the linked issue comments are very similar yes

@ianlancetaylor ianlancetaylor changed the title proposal: golang.org/x/exp: new maps.Any API proposal: maps: add Any function May 30, 2024
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals May 30, 2024
@ianlancetaylor
Copy link
Member

Thanks, I'm going to close this as a dup of #66361.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants